欢迎来到天天文库
浏览记录
ID:34714549
大小:47.72 KB
页数:4页
时间:2019-03-10
《数据结构图的遍历实验深度优先》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、#include#include#defineMAX_VERTEX_NUM20typedefenum{DG,AG}GraphKind;typedefstructArcCell{intadj;//VRType是顶点关系类型。对无权图,用1或0表示相邻否;对带全图,则为权值类型;//该弧相关信息的指针s}ArcCell,AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];typedefstruct{intvexs[MAX_VERTEX_NUM];//顶点向量AdjMatrixarcs;//邻接矩阵intvexnum,
2、arcnum;//图的当前顶点数和弧数GraphKindkind;//图的种类标志}MGraph;MGraphG;//----------------------队列的定义--------------------------------typedefstructQNode{intdata;structQNode*next;}QNode,*QueuePtr;typedefstruct{QueuePtrfront;//队头指针QueuePtrrear;//队尾指针}LinkQueue;LinkQueueQ;intvisited[MAX_VERTEX_NUM];voidVisitFunc
3、(intv){printf("%d",G.vexs[v]);}intFirstAdjVex(MGraphG,intv){intj;for(j=0;j4、urn0;}intLocateVex(MGraphG,intv1){inti;for(i=0;i5、/构造顶点向量for(i=0;i6、连通图Gvisited[v]=1;VisitFunc(v);for(w=FirstAdjVex(G,v);w!=0;w=NextAdjVex(G,v))if(!visited[w])DFS(G,w);//对v的尚未访问的邻接顶点w//递归调用DFS}//DFSvoidEnQueue(LinkQueue&Q,intv)//V入队列{Q.rear->data=v;Q.rear++;}voidInitQueue(LinkQueue&Q)//至空辅助队列{Q.front=Q.rear;}voidDeQueue(LinkQueue&Q,int&v){//队头元素出队并置为vv=Q.front7、->data;Q.front++;}intQueueEmpty(LinkQueueQ){if(Q.front==Q.rear)return0;elsereturn1;}voidVisit(intv){printf("%d",v);}voidBFS(MGraphG){intv,w;for(v=0;v
4、urn0;}intLocateVex(MGraphG,intv1){inti;for(i=0;i5、/构造顶点向量for(i=0;i6、连通图Gvisited[v]=1;VisitFunc(v);for(w=FirstAdjVex(G,v);w!=0;w=NextAdjVex(G,v))if(!visited[w])DFS(G,w);//对v的尚未访问的邻接顶点w//递归调用DFS}//DFSvoidEnQueue(LinkQueue&Q,intv)//V入队列{Q.rear->data=v;Q.rear++;}voidInitQueue(LinkQueue&Q)//至空辅助队列{Q.front=Q.rear;}voidDeQueue(LinkQueue&Q,int&v){//队头元素出队并置为vv=Q.front7、->data;Q.front++;}intQueueEmpty(LinkQueueQ){if(Q.front==Q.rear)return0;elsereturn1;}voidVisit(intv){printf("%d",v);}voidBFS(MGraphG){intv,w;for(v=0;v
5、/构造顶点向量for(i=0;i6、连通图Gvisited[v]=1;VisitFunc(v);for(w=FirstAdjVex(G,v);w!=0;w=NextAdjVex(G,v))if(!visited[w])DFS(G,w);//对v的尚未访问的邻接顶点w//递归调用DFS}//DFSvoidEnQueue(LinkQueue&Q,intv)//V入队列{Q.rear->data=v;Q.rear++;}voidInitQueue(LinkQueue&Q)//至空辅助队列{Q.front=Q.rear;}voidDeQueue(LinkQueue&Q,int&v){//队头元素出队并置为vv=Q.front7、->data;Q.front++;}intQueueEmpty(LinkQueueQ){if(Q.front==Q.rear)return0;elsereturn1;}voidVisit(intv){printf("%d",v);}voidBFS(MGraphG){intv,w;for(v=0;v
6、连通图Gvisited[v]=1;VisitFunc(v);for(w=FirstAdjVex(G,v);w!=0;w=NextAdjVex(G,v))if(!visited[w])DFS(G,w);//对v的尚未访问的邻接顶点w//递归调用DFS}//DFSvoidEnQueue(LinkQueue&Q,intv)//V入队列{Q.rear->data=v;Q.rear++;}voidInitQueue(LinkQueue&Q)//至空辅助队列{Q.front=Q.rear;}voidDeQueue(LinkQueue&Q,int&v){//队头元素出队并置为vv=Q.front
7、->data;Q.front++;}intQueueEmpty(LinkQueueQ){if(Q.front==Q.rear)return0;elsereturn1;}voidVisit(intv){printf("%d",v);}voidBFS(MGraphG){intv,w;for(v=0;v
此文档下载收益归作者所有