您的位置:首页 > 编程语言 > C语言/C++

基于链表的图搜索

2016-07-02 17:56 260 查看
#include<iostream>
#include<iomanip>
using namespace std;
//构造一个循环队列来存放广度优先算法的下标
#define ADD 5;
using namespace std;

class CirQueue
{
private:
    int * base;
    int front,rear,size,length;
public:
   bool InitCirQueue(int size)
   {
this->size=size;
base=new int[size];
if(!base)
{
return false;
}
front=rear=length=0;
return true;
}
bool insertQueue(int num)
{
if(length==size)
{
int newsize=size+ADD;
int * newbase=new int[newsize];
if(!newbase)
{
return false;
}
int i=0;
for(i=0;i<length;i++)
{
newbase[(front+i)%newsize]=base[(front+i)%size];
}
rear=(front+i)%newsize;
base=newbase;
size=newsize;
}
base[rear]=num;
rear=(rear+1)%size;
++length;
return true;
}
int outQueue()
{
int temp=base[front];
front=(front+1)%size;
--length;
return temp;
}
void traverseQueue()
{
for(int i=0;i<length;i++)
{
cout << "V" << base[(front + i) % size]<<" , ";
}
}
int getLength()
{
return length;
}
bool isEmpty()
{
if(0==length)
{
return true;
}
else
{
return false;
}
}
bool isFull()
{
if(length==size)
{
return true;
}
else
{
return false;
}
}
};
 
struct Arc
{
    int adjvex;
    Arc * next;
};
 
struct Vertex
{
    char data;
    Arc * firstarc;
};
 
class Map
{
private:
    Vertex * vexList;
    int vexNum;
    int arcNum;
    bool * visted;
public:
    Map(int vexNum,int arcNum)
    {
this->vexNum=vexNum;
        this->arcNum=arcNum;
        visted=new bool[vexNum];
        for(int i=0;i<vexNum;i++)
        {
        visted[i]=false;
        }
        vexList=new Vertex[vexNum];
cout << endl;
        for(int i=1;i<=vexNum;i++)
        {
            cout<<"  请输入第"<<i<<"个顶点的数据:V";
            cin>>vexList[i-1].data;
vexList[i-1].firstarc=NULL;
        }
cout << endl;
for(int i=0;i<arcNum;i++)
        {
            int a,b;
            cout<<"  请输入第"<<i+1<<"条边的两顶点";
cout << endl;
cout << "  结点:V";
cin >> a;
cout << "  结点:V";
cin>>b;
cout << endl;
Arc * tempArc=new Arc;
            tempArc->adjvex=b;
            tempArc->next=vexList[a].firstarc;
vexList[a].firstarc=tempArc;
            
            //因为是无向图所以是双向的
            tempArc=new Arc;
            tempArc->adjvex=a;
            tempArc->next=vexList[b].firstarc;
            vexList[b].firstarc=tempArc;
        };
    }
     void DFS(int v)
     {
cout << "V" << vexList[v].data <<" , ";
         visted[v]=true;
         Arc * p=vexList[v].firstarc;
         while(p)
         {
             int u=p->adjvex;
             if(!visted[u])
             {
                 DFS(u);
             }
             p=p->next;
         }
     }
     void BFS(int v)
     {
         CirQueue cq;
         cq.InitCirQueue(5);
cout << "V" << vexList[v].data << " , ";
         visted[v]=true;
         cq.insertQueue(v);
         while(!cq.isEmpty())
         {
             v=cq.outQueue();
             Arc * p=vexList[v].firstarc;
             while(p)
             {
                int j=p->adjvex;
                 if(!visted[j])
                {
cout << "V" << vexList[j].data << " , ";
                    visted[j]=true;
cq.insertQueue(j);
                }
p=p->next;
            }
         }
     }
     void ClearVisted()
     {
        for(int i=0;i<vexNum;i++)
        {
            visted[i]=false;
        }
     }
 };
 int main()
{
Map map(5,6);//五个定点六条边
cout << endl;
cout<<"  深度优先遍历:";
map.DFS(0);
cout << endl;
map.ClearVisted();
cout<<"  广度优先遍历:";
map.BFS(0);
cout << endl;
cout << endl;
cout << endl;
system("pause");
return 0;
 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息