欢迎来到天天文库
浏览记录
ID:36569258
大小:36.00 KB
页数:5页
时间:2019-05-12
《建立无向图的邻接多重表》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、题目:编写一个算法由依次输入的顶点数目,边的数目,各顶点的信息和各条边的信息建立无向图的邻接多重表。一.需求分析这里需要两个主要字函数,一个是建立图,另一个是打印图。二.概要设计首先是建立两个结点,一个是边结点,另一个是顶点结点,分别为structEdge,structNode,然后建立图,Create_ML_Graph(intVertex1,NextEdgeNew),紧接着是打印Print_ML_Graph(structNode*Head)。三.详细设计#include#inc
2、lude#defineVertexNum6#defineNULL((void*)0)structEdge{intMarked;intVertex1;intVertex2;structEdge*Edge1;structEdge*Edge2;};typedefstructEdge*NextEdge;structNode{intVertex;structEdge*Next;};typedefstructNode*Graph;structNodeHead[VertexNum];voidCr
3、eate_ML_Graph(intVertex1,NextEdgeNew){NextEdgePointer;NextEdgePrevious;Previous=NULL;Pointer=Head[Vertex1].Next;while(Pointer!=NULL){Previous=Pointer;if(Pointer->Vertex1==Vertex1)Pointer=Pointer->Edge1;elsePointer=Pointer->Edge2;}if(Previous==NULL)Head
4、[Vertex1].Next=New;elseif(Previous->Vertex1==Vertex1)Previous->Edge1=New;elsePrevious->Edge2=New;}voidPrint_ML_Graph(structNode*Head){NextEdgePointer;Pointer=Head->Next;while(Pointer!=NULL){printf("(%d,%d)",Pointer->Vertex1,Pointer->Vertex2);if(Head->V
5、ertex==Pointer->Vertex1)Pointer=Pointer->Edge1;elseif(Head->Vertex==Pointer->Vertex2)Pointer=Pointer->Edge2;}printf("");}voidmain(){intSource;intDestinition;intChoose;NextEdgeNew;inti;for(i=0;i6、f("1.UndirectedGraph");printf("2.DirectedGraph");printf("Pleasechoose:");scanf("%d",&Choose);while(1){printf("PleaseinputtheEdge'ssource:");scanf("%d",&Source);if(Source==-1)break;printf("PleaseinputtheEdge'sDestinition:");scanf("%d",&Destinition);7、if(Source>=VertexNum8、9、Destinition>=VertexNum)printf("@Error@:outofrange!!");else{New=(NextEdge)malloc(sizeof(structEdge));if(New!=NULL){New->Vertex1=Source;New->Vertex2=Destinition;New->Edge1=NULL;New->Edge2=NULL;Create_ML_Graph(Destinition,New);}}}p10、rintf("##Graph##");for(i=0;i
6、f("1.UndirectedGraph");printf("2.DirectedGraph");printf("Pleasechoose:");scanf("%d",&Choose);while(1){printf("PleaseinputtheEdge'ssource:");scanf("%d",&Source);if(Source==-1)break;printf("PleaseinputtheEdge'sDestinition:");scanf("%d",&Destinition);
7、if(Source>=VertexNum
8、
9、Destinition>=VertexNum)printf("@Error@:outofrange!!");else{New=(NextEdge)malloc(sizeof(structEdge));if(New!=NULL){New->Vertex1=Source;New->Vertex2=Destinition;New->Edge1=NULL;New->Edge2=NULL;Create_ML_Graph(Destinition,New);}}}p
10、rintf("##Graph##");for(i=0;i
此文档下载收益归作者所有