그림의 반복 (인접표 구현)

1970 단어
#include
#include
using namespace std;

typedef char DataType;
#define MAXV 10// 
#define MAXE 50// 
int visited[MAXV]={0};

typedef struct ArcNode{
    int adjvex;// 
    ArcNode* next;
}ArcNode;

typedef struct VertexNode{
    DataType vertex;
    ArcNode*firstE;
}VertexNode;

typedef struct MyGraph{
VertexNode adjlist[MAXV];
    int VNum;
    int ENum;
}MyGraph;

void CreatGraph(MyGraph *G);
void DFS(MyGraph* G,int v);
void DFSTraverse(MyGraph *G);
void BFSTraverse(MyGraph* G,int v);

void CreatGraph(MyGraph *G)
{
    int i,j,k;
    cout<>G->VNum>>G->ENum;

    cout<VNum;i++)// 
    {
        cin>>G->adjlist[i].vertex;
        G->adjlist[i].firstE=NULL;
    }
    for(k=0;kENum;k++)
    {
        cout<>i>>j;

        ArcNode*s=new ArcNode; s->adjvex=j;
        s->next=G->adjlist[i].firstE;
        G->adjlist[i].firstE=s;
    }
}

void DFS(MyGraph *G,int v)
{
    ArcNode *p;
    int j;
    cout<adjlist[v].vertex<adjlist[v].firstE;
    while(p!=NULL)
    {
        j=p->adjvex;
        if(visited[j]==0) DFS(G,j);
        p=p->next;
    }
}

void DFSTraverse(MyGraph* G)
{
    ArcNode *p;
    int i,j;
    for(i=0;iVNum;i++) visited[i]=0;

    for(i=0;iVNum;i++)
        {
            p=G->adjlist[i].firstE;
            if(p!=NULL&&visited[i]==0) DFS(G,i);
        }
}

void BFSTraverse(MyGraph* G,int v)
{
    int j;
    ArcNode *p;
    queue q;
    cout<adjlist[v].vertex<adjlist[v].firstE;
        while(p!=NULL)
        {
            j=p->adjvex;
            if(visited[j]==0){
                cout<adjlist[j].vertex;
                visited[j]=1;
                q.front()=j;
            }
        }
    }
}

int main()
{
    MyGraph* G=new MyGraph;
    CreatGraph(G);

    cout<VNum;i++)
    BFSTraverse(G,i);

    return 0;

}

좋은 웹페이지 즐겨찾기