欢迎来到天天文库
浏览记录
ID:49950410
大小:263.50 KB
页数:25页
时间:2020-03-03
《数据结构邻接矩阵,邻接表,图实验报告.doc》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、.实验名称:数据结构实验五实验内容:1.使用邻接矩阵建立一个图,深度遍历。2.使用邻接表建立一个图,广度遍历。3.建立一个图,存储结构自己确定,并进行拓扑排序。实验代码:1.#include"stdio.h"#defineInfinity100#defineMaxVertexNum20typedefenum{DG,DN,UDG,UDN}GraphKind;typedefintVRType;typedefcharVertexType;boolVisit[MaxVertexNum];typedefstructArcCell{VRTypeadj;}ArcCell,AdjMatrix[Ma
2、xVertexNum][MaxVertexNum];typedefstruct{VertexTypevexs[MaxVertexNum];AdjMatrixarcs;//邻接矩阵intvexnum,arcnum;//图的当前顶点数和弧数Word资料.GraphKindkind;}MGraph;intLocateVex(MGraphG,VertexTypev){for(inti=0;i3、RTypew;voidCreateUDG(MGraph&G){Word资料.inti,j,k;printf("请输入顶点数:");scanf("%d",&G.vexnum);printf("请输入弧数:");scanf("%d",&G.arcnum);i=0;while(i4、rcnum;++k){printf("请输入一条边依附的顶点及权值(v1v2w)");getchar();Word资料.scanf("%c%c%d",&v1,&v2,&w);i=LocateVex(G,v1);j=LocateVex(G,v2);G.arcs[i][j].adj=w;G.arcs[j][i]=G.arcs[i][j];}return;}voidDFSTraverse(MGraph&G,inti){printf("%c",G.vexs[i]);Visit[i]=true;for(intj=0;j5、=1&&!Visit[j]){DFSTraverse(G,j);}}Word资料.}voidDFS(MGraph&G){inti;for(i=0;i6、度遍历结果是:");DFS(graph);printf("");return;}2.#include"stdio.h"#include"stdlib.h"#defineMaxVertexNum20typedefintInfoType;typedefcharVertexType;typedefVertexTypeQElemType;boolvisited[MaxVertexNum];typedefstructArcNode{intadjvex;//该弧指向的顶点位置structArcNode*nextarc;//指向下一条弧的指针InfoType*info;}ArcNode;Wo7、rd资料.typedefstructVNode{VertexTypedata;//顶点信息ArcNode*firstarc;//指向第一条依附该顶点的弧的指针}VNode,AdjList[MaxVertexNum];typedefstruct{AdjListvertices;intvexnum,arcnum;//图的当前顶点数和弧数}ALGraph;typedefstructQNode{QElemTypedata;structQNode*next;}QNode,*Qu
3、RTypew;voidCreateUDG(MGraph&G){Word资料.inti,j,k;printf("请输入顶点数:");scanf("%d",&G.vexnum);printf("请输入弧数:");scanf("%d",&G.arcnum);i=0;while(i4、rcnum;++k){printf("请输入一条边依附的顶点及权值(v1v2w)");getchar();Word资料.scanf("%c%c%d",&v1,&v2,&w);i=LocateVex(G,v1);j=LocateVex(G,v2);G.arcs[i][j].adj=w;G.arcs[j][i]=G.arcs[i][j];}return;}voidDFSTraverse(MGraph&G,inti){printf("%c",G.vexs[i]);Visit[i]=true;for(intj=0;j5、=1&&!Visit[j]){DFSTraverse(G,j);}}Word资料.}voidDFS(MGraph&G){inti;for(i=0;i6、度遍历结果是:");DFS(graph);printf("");return;}2.#include"stdio.h"#include"stdlib.h"#defineMaxVertexNum20typedefintInfoType;typedefcharVertexType;typedefVertexTypeQElemType;boolvisited[MaxVertexNum];typedefstructArcNode{intadjvex;//该弧指向的顶点位置structArcNode*nextarc;//指向下一条弧的指针InfoType*info;}ArcNode;Wo7、rd资料.typedefstructVNode{VertexTypedata;//顶点信息ArcNode*firstarc;//指向第一条依附该顶点的弧的指针}VNode,AdjList[MaxVertexNum];typedefstruct{AdjListvertices;intvexnum,arcnum;//图的当前顶点数和弧数}ALGraph;typedefstructQNode{QElemTypedata;structQNode*next;}QNode,*Qu
4、rcnum;++k){printf("请输入一条边依附的顶点及权值(v1v2w)");getchar();Word资料.scanf("%c%c%d",&v1,&v2,&w);i=LocateVex(G,v1);j=LocateVex(G,v2);G.arcs[i][j].adj=w;G.arcs[j][i]=G.arcs[i][j];}return;}voidDFSTraverse(MGraph&G,inti){printf("%c",G.vexs[i]);Visit[i]=true;for(intj=0;j5、=1&&!Visit[j]){DFSTraverse(G,j);}}Word资料.}voidDFS(MGraph&G){inti;for(i=0;i6、度遍历结果是:");DFS(graph);printf("");return;}2.#include"stdio.h"#include"stdlib.h"#defineMaxVertexNum20typedefintInfoType;typedefcharVertexType;typedefVertexTypeQElemType;boolvisited[MaxVertexNum];typedefstructArcNode{intadjvex;//该弧指向的顶点位置structArcNode*nextarc;//指向下一条弧的指针InfoType*info;}ArcNode;Wo7、rd资料.typedefstructVNode{VertexTypedata;//顶点信息ArcNode*firstarc;//指向第一条依附该顶点的弧的指针}VNode,AdjList[MaxVertexNum];typedefstruct{AdjListvertices;intvexnum,arcnum;//图的当前顶点数和弧数}ALGraph;typedefstructQNode{QElemTypedata;structQNode*next;}QNode,*Qu
5、=1&&!Visit[j]){DFSTraverse(G,j);}}Word资料.}voidDFS(MGraph&G){inti;for(i=0;i6、度遍历结果是:");DFS(graph);printf("");return;}2.#include"stdio.h"#include"stdlib.h"#defineMaxVertexNum20typedefintInfoType;typedefcharVertexType;typedefVertexTypeQElemType;boolvisited[MaxVertexNum];typedefstructArcNode{intadjvex;//该弧指向的顶点位置structArcNode*nextarc;//指向下一条弧的指针InfoType*info;}ArcNode;Wo7、rd资料.typedefstructVNode{VertexTypedata;//顶点信息ArcNode*firstarc;//指向第一条依附该顶点的弧的指针}VNode,AdjList[MaxVertexNum];typedefstruct{AdjListvertices;intvexnum,arcnum;//图的当前顶点数和弧数}ALGraph;typedefstructQNode{QElemTypedata;structQNode*next;}QNode,*Qu
6、度遍历结果是:");DFS(graph);printf("");return;}2.#include"stdio.h"#include"stdlib.h"#defineMaxVertexNum20typedefintInfoType;typedefcharVertexType;typedefVertexTypeQElemType;boolvisited[MaxVertexNum];typedefstructArcNode{intadjvex;//该弧指向的顶点位置structArcNode*nextarc;//指向下一条弧的指针InfoType*info;}ArcNode;Wo
7、rd资料.typedefstructVNode{VertexTypedata;//顶点信息ArcNode*firstarc;//指向第一条依附该顶点的弧的指针}VNode,AdjList[MaxVertexNum];typedefstruct{AdjListvertices;intvexnum,arcnum;//图的当前顶点数和弧数}ALGraph;typedefstructQNode{QElemTypedata;structQNode*next;}QNode,*Qu
此文档下载收益归作者所有