遍历图C++

来源:互联网 发布:jd是什么意思网络用语 编辑:程序博客网 时间:2024/06/05 22:33
#include<iostream>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<<base[(front+i)%size]<<endl;        }    }    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];        for(int i=0;i<vexNum;i++)        {            cout<<"请输入第"<<i<<"个顶点的数据:";            cin>>vexList[i].data;            vexList[i].firstarc=NULL;        }        for(int i=0;i<arcNum;i++)        {            int a,b;            cout<<"请输入第"<<i+1<<"条边的两顶点:";            cin>>a>>b;            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<<vexList[v].data<<endl;        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<<vexList[v].data<<endl;        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<<vexList[j].data<<endl;                    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,4);    cout<<"--------------深度优先遍历————————————————"<<endl;    map.DFS(0);    map.ClearVisted();    cout<<"--------------广度优先遍历————————————————"<<endl;    map.BFS(0);    return 0;}
原创粉丝点击