1、图的深度遍历和广度遍历程序源代码如下:#include“stdio.h“#include“stdlib.h“#define MAXQSIZE 100#define MVNum 100int visited100;typedef structchar *base;int front;int rear;SqQueue;int InitQueue(SqQueue if(!Q.base)exit(-1); /存储失败Q.front=Q.rear=0; /队列为空return 1;int QueueEmpty(SqQueue return 1;int EnQueue(SqQueue Q.baseQ.re
2、ar=e; /新元素插入队尾Q.rear=(Q.rear+1)%MAXQSIZE; /队尾指针加 1return 1;int DeQueue(SqQueue /队空e=Q.baseQ.front;Q.front=(Q.front+1)%MAXQSIZE;return 1;int GetHead(SqQueue Q) /取队头元素if(Q.front!=Q.rear)return Q.baseQ.front;typedef struct /定义图char vexsMVNum;int arcsMVNumMVNum;int vexnum,arcnum;AMGraph;int LocateVex(AM
3、Graph G,char u)int i;for(i=0;iG.vexnum;+i)if(u=G.vexsi)return i;return -1;int CreateUDN(AMGraph char v1,v2;printf(“输入顶点个数和边的个数:n“);scanf(“%d%d“,getchar();printf(“请输入图:n“);for(i=0;iG.vexnum;+i)scanf(“%c“,getchar();for(i=0;iG.vexnum;+i)for(j=0;jG.vexnum;+j)G.arcsij=0;for(k=0;kG.arcnum;+k)scanf(“%c%c“,
4、getchar();i=LocateVex(G,v1);j=LocateVex(G,v2);G.arcsij=1;G.arcsji=G.arcsij;return 1;void DFS(AMGraph G,int v) /深度遍历图int w;printf(“%c“,G.vexsv);visitedv=1;for(w=0;wG.vexnum;w+)if(G.arcsvw!=0)void DFSTraverse(AMGraph G)int v;for(v=0;vG.vexnum;+v)visitedv=0;for(v=0;vG.vexnum;+v)if(!visitedv)DFS(G,v);vo
5、id BFS(AMGraph G,int v) /广度遍历int w;SqQueue Q;printf(“%c“,G.vexsv); /访问第 v 个顶点visitedv=1;InitQueue(Q); /初始化队列,置空EnQueue(Q,v); /v 进队char u=GetHead(Q);while(!QueueEmpty(Q) /队列非空DeQueue(Q,u); /队头元素出队 ,置为 ufor(w=0;wG.vexsv;w+)if(visitedw)scanf(“%d“,visitedw=1;EnQueue(Q,w); /w 进队void BFSTraverse(AMGraph G) int v;for(v=0;vG.vexnum;+v)visitedv=0;for(v=0;vG.vexnum;+v)if(!visitedv)BFS(G,v);main()AMGraph G;CreateUDN(G);printf(“深度遍历结果:n“);DFSTraverse(G);printf(“n 广度遍历结果:n“);BFSTraverse(G);printf(“n“);输入的图如下: ab cd e f g深度遍历结果:abdecfg广度遍历结果:abcdefg