欢迎来到天天文库
浏览记录
ID:69502822
大小:249.50 KB
页数:25页
时间:2021-11-06
《数据结构邻接矩阵,邻接表,图实验报告》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、.-实验名称:数据构造实验五实验内容:1.使用邻接矩阵建立一个图,深度遍历。2.使用邻接表建立一个图,广度遍历。3.建立一个图,存储构造自己确定,并进展拓扑排序。实验代码:1.#include"stdio.h"#defineInfinity100#defineMaxVertexNum20typedefenum{DG,DN,UDG,UDN}GraphKind;typedefintVRType;typedefcharVertexType;boolVisit[MaxVertexNum];typedefstructArcCell{VRTypeadj;}ArcCell,AdjMa
2、trix[MaxVertexNum][MaxVertexNum];typedefstruct{VertexTypevexs[MaxVertexNum];AdjMatrixarcs;//邻接矩阵intvexnum,arum;//图的当前顶点数和弧数-.word.zl.-GraphKindkind;}MGraph;intLocateVex(MGraphG,VertexTypev){for(inti=0;i3、rtexTypev1,v2;VRTypew;voidCreateUDG(MGraph&G){-.word.zl.-inti,j,k;printf("请输入顶点数:");scanf("%d",&G.vexnum);printf("请输入弧数:");scanf("%d",&G.arum);i=0;while(i4、j].adj=0;}for(k=0;k5、exnum;j++){if(G.arcs[i][j].adj==1&&!Visit[j]){DFSTraverse(G,j);}}-.word.zl.-}voidDFS(MGraph&G){inti;for(i=0;i6、rd.zl.-printf("%c",graph.vexs[i]);printf("深度遍历结果是:");DFS(graph);printf("");return;}2.#include"stdio.h"#include"stdlib.h"#defineMaxVertexNum20typedefintInfoType;typedefcharVertexType;typedefVertexTypeQElemType;boolvisited[MaxVertexNum];typedefstructArode{intadjvex;//该弧指向的顶点位置structAro7、de*nextarc;//指向下一条弧的指针InfoType*info;}Arode;-.word.zl.-typedefstructVNode{VertexTypedata;//顶点信息Arode*firstarc;//指向第一条依附该顶点的弧的指针}VNode,AdjList[MaxVertexNum];typedefstruct{AdjListvertices;intvexnum,arum;//图的当前顶点数和弧数}ALGraph;typedefstructQNode{QElemTypedata;structQNode*next;}
3、rtexTypev1,v2;VRTypew;voidCreateUDG(MGraph&G){-.word.zl.-inti,j,k;printf("请输入顶点数:");scanf("%d",&G.vexnum);printf("请输入弧数:");scanf("%d",&G.arum);i=0;while(i4、j].adj=0;}for(k=0;k5、exnum;j++){if(G.arcs[i][j].adj==1&&!Visit[j]){DFSTraverse(G,j);}}-.word.zl.-}voidDFS(MGraph&G){inti;for(i=0;i6、rd.zl.-printf("%c",graph.vexs[i]);printf("深度遍历结果是:");DFS(graph);printf("");return;}2.#include"stdio.h"#include"stdlib.h"#defineMaxVertexNum20typedefintInfoType;typedefcharVertexType;typedefVertexTypeQElemType;boolvisited[MaxVertexNum];typedefstructArode{intadjvex;//该弧指向的顶点位置structAro7、de*nextarc;//指向下一条弧的指针InfoType*info;}Arode;-.word.zl.-typedefstructVNode{VertexTypedata;//顶点信息Arode*firstarc;//指向第一条依附该顶点的弧的指针}VNode,AdjList[MaxVertexNum];typedefstruct{AdjListvertices;intvexnum,arum;//图的当前顶点数和弧数}ALGraph;typedefstructQNode{QElemTypedata;structQNode*next;}
4、j].adj=0;}for(k=0;k5、exnum;j++){if(G.arcs[i][j].adj==1&&!Visit[j]){DFSTraverse(G,j);}}-.word.zl.-}voidDFS(MGraph&G){inti;for(i=0;i6、rd.zl.-printf("%c",graph.vexs[i]);printf("深度遍历结果是:");DFS(graph);printf("");return;}2.#include"stdio.h"#include"stdlib.h"#defineMaxVertexNum20typedefintInfoType;typedefcharVertexType;typedefVertexTypeQElemType;boolvisited[MaxVertexNum];typedefstructArode{intadjvex;//该弧指向的顶点位置structAro7、de*nextarc;//指向下一条弧的指针InfoType*info;}Arode;-.word.zl.-typedefstructVNode{VertexTypedata;//顶点信息Arode*firstarc;//指向第一条依附该顶点的弧的指针}VNode,AdjList[MaxVertexNum];typedefstruct{AdjListvertices;intvexnum,arum;//图的当前顶点数和弧数}ALGraph;typedefstructQNode{QElemTypedata;structQNode*next;}
5、exnum;j++){if(G.arcs[i][j].adj==1&&!Visit[j]){DFSTraverse(G,j);}}-.word.zl.-}voidDFS(MGraph&G){inti;for(i=0;i6、rd.zl.-printf("%c",graph.vexs[i]);printf("深度遍历结果是:");DFS(graph);printf("");return;}2.#include"stdio.h"#include"stdlib.h"#defineMaxVertexNum20typedefintInfoType;typedefcharVertexType;typedefVertexTypeQElemType;boolvisited[MaxVertexNum];typedefstructArode{intadjvex;//该弧指向的顶点位置structAro7、de*nextarc;//指向下一条弧的指针InfoType*info;}Arode;-.word.zl.-typedefstructVNode{VertexTypedata;//顶点信息Arode*firstarc;//指向第一条依附该顶点的弧的指针}VNode,AdjList[MaxVertexNum];typedefstruct{AdjListvertices;intvexnum,arum;//图的当前顶点数和弧数}ALGraph;typedefstructQNode{QElemTypedata;structQNode*next;}
6、rd.zl.-printf("%c",graph.vexs[i]);printf("深度遍历结果是:");DFS(graph);printf("");return;}2.#include"stdio.h"#include"stdlib.h"#defineMaxVertexNum20typedefintInfoType;typedefcharVertexType;typedefVertexTypeQElemType;boolvisited[MaxVertexNum];typedefstructArode{intadjvex;//该弧指向的顶点位置structAro
7、de*nextarc;//指向下一条弧的指针InfoType*info;}Arode;-.word.zl.-typedefstructVNode{VertexTypedata;//顶点信息Arode*firstarc;//指向第一条依附该顶点的弧的指针}VNode,AdjList[MaxVertexNum];typedefstruct{AdjListvertices;intvexnum,arum;//图的当前顶点数和弧数}ALGraph;typedefstructQNode{QElemTypedata;structQNode*next;}
此文档下载收益归作者所有