用c++实现图的两种遍历

#include <stdio.h>

#include <malloc.h>

#define INF 32767

#define  MAXV 100

typedef char InfoType;

typedef struct

{     int no;

       InfoType info;

} VertexType;

typedef struct

{     int edges[MAXV][MAXV];

       int n,e;

       VertexType vexs[MAXV];

} MatGraph;

typedef struct ANode

{     int adjvex;

       struct ANode *nextarc;

       int weight;

} ArcNode;

typedef struct Vnode

{     InfoType info;

       int count;

       ArcNode *firstarc;

} VNode;

typedef struct

{     VNode adjlist[MAXV];

       int n,e;

} AdjGraph;

void CreateMat(MatGraph &g,int A[MAXV][MAXV],int n,int e)

{

       int i,j;

       g.n=n; g.e=e;

       for (i=0;i<g.n;i++)

              for (j=0;j<g.n;j++)

                     g.edges[i][j]=A[i][j];

}

void DispMat(MatGraph g)

{

       int i,j;

       for (i=0;i<g.n;i++)

       {

              for (j=0;j<g.n;j++)

                     if (g.edges[i][j]!=INF)

                            printf("%4d",g.edges[i][j]);

                     else

                            printf("%4s","∞");

              printf("\n");

       }

}

void CreateAdj(AdjGraph *&G,int A[MAXV][MAXV],int n,int e)

{

       int i,j;

       ArcNode *p;

       G=(AdjGraph *)malloc(sizeof(AdjGraph));

       for (i=0;i<n;i++)

              G->adjlist[i].firstarc=NULL;

       for (i=0;i<n;i++)

              for (j=n-1;j>=0;j--)

                     if (A[i][j]!=0 && A[i][j]!=INF)

                     {     p=(ArcNode *)malloc(sizeof(ArcNode));

                            p->adjvex=j;

                            p->weight=A[i][j];

                            p->nextarc=G->adjlist[i].firstarc;

                            G->adjlist[i].firstarc=p;

                     }

       G->n=n; G->e=n;

}

void DispAdj(AdjGraph *G)

{

       ArcNode *p;

       for (int i=0;i<G->n;i++)

       {

              p=G->adjlist[i].firstarc;

              printf("%3d: ",i);

              while (p!=NULL)

              {

                     printf("%3d[%d]→",p->adjvex,p->weight);

                     p=p->nextarc;

              }

              printf("∧\n");

       }

}

void DestroyAdj(AdjGraph *&G)

{

       ArcNode *pre,*p;

       for (int i=0;i<G->n;i++)

       {     pre=G->adjlist[i].firstarc;

              if (pre!=NULL)

              {     p=pre->nextarc;

                     while (p!=NULL)

                     {     free(pre);

                            pre=p; p=p->nextarc;

                     }

                     free(pre);

              }

       }

       free(G);

}

int visited[MAXV];

void DFS(AdjGraph *G,int v)

{

       ArcNode *p;

       visited[v]=1;

       printf("%3d",v);

       p=G->adjlist[v].firstarc;

       while (p!=NULL)

       {

              if (visited[p->adjvex]==0)

                     DFS(G,p->adjvex);

              p=p->nextarc;

       }

}

void DFS1(AdjGraph *G,int v)

{

       ArcNode *p;

       int St[MAXV];

       int top=-1,w,x,i;

    for (i=0;i<G->n;i++)

              visited[i]=0;

       printf("%3d",v);

       visited[v]=1;

       top++; St[top]=v;

       while (top>-1)

       {

              x=St[top];

              p=G->adjlist[x].firstarc;

              while (p!=NULL)

              {

                     w=p->adjvex;

                     if (visited[w]==0)

                     {

                            printf("%3d",w);

                            visited[w]=1;

                            top++;

                            St[top]=w;

                            break;

                     }

                     p=p->nextarc;

              }

              if (p==NULL) top--;

       }

       printf("\n");

}

void BFS(AdjGraph *G,int v)

{

       ArcNode *p;

       int queue[MAXV],front=0,rear=0;

       int visited[MAXV];

       int w,i;

       for (i=0;i<G->n;i++) visited[i]=0;

       printf("%3d",v);

       visited[v]=1;

       rear=(rear+1)%MAXV;

       queue[rear]=v;

       while (front!=rear)

       {

              front=(front+1)%MAXV;

              w=queue[front];

              p=G->adjlist[w].firstarc;

              while (p!=NULL)

              {

                     if (visited[p->adjvex]==0)

                     {

                            printf("%3d",p->adjvex);

                            visited[p->adjvex]=1;

                            rear=(rear+1)%MAXV;

                            queue[rear]=p->adjvex;

                 }

                 p=p->nextarc;

              }

       }

       printf("\n");

}

int main()

{

       AdjGraph *G;

       int A[MAXV][MAXV]={

              {0,5,INF,7,INF,INF},

              {INF,0,4,INF,INF,INF},

              {8,INF,0,INF,INF,9},

              {INF,INF,5,0,INF,6},

              {INF,INF,INF,5,0,INF},

              {3,INF,INF,INF,1,0}};

       int n=6,e=10;

       CreateAdj(G,A,n,e);

       printf("图G的邻接表:\n"); DispAdj(G);

       printf("从顶点0开始的DFS(递归算法):\n");

       DFS(G,0);printf("\n");

       printf("从顶点0开始的DFS(非递归算法):\n");

       DFS1(G,0);

       printf("从顶点0开始的BFS:\n");

       BFS(G,0);

       DestroyAdj(G);

       return 1;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值