欢迎来到天天文库
浏览记录
ID:13666667
大小:40.50 KB
页数:6页
时间:2018-07-23
《图的邻接表和邻接矩阵的相互转换》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、#include#include#includetypedefintInfoType;typedefintVertex;#defineMAXV5typedefstruct{intno;InfoTypeinfo;}VertexType;typedefstruct{intedges[MAXV][MAXV];intn,e;VertexTypevexs[MAXV];}MGraph;typedefstructANode{intadjvex;structANode*nextarc;InfoTypeinfo;}ArcNode;typede
2、fstructVnode{Vertexdata;ArcNode*firstarc;}VNode;typedefVNodeAdjList[MAXV];typedefstruct{AdjListadjlist;intn,e;}ALGraph;voidInitialMG(MGraph*&MG){MG=newMGraph;MG->e=0;MG->n=5;intj;for(inti=0;ivexs[i].no=i;for(i=0;iedges[i][j]=0;}voidInitialALG(ALGrap
3、h*&ALG){ALG=newALGraph;ALG->n=5;ALG->e=0;for(inti=0;iadjlist[i].data=i;ALG->adjlist[i].firstarc=NULL;}}voidInputALG(ALGraph*&ALG){intch;ArcNode*p;cout<n;i++){cout<adjlist[i].data<<":";cin>>ch;if(ch!=-1){ALG->adjl
4、ist[i].firstarc=newArcNode;p=ALG->adjlist[i].firstarc;p->adjvex=ch;p->nextarc=NULL;cin>>ch;}while(ch!=-1){p->nextarc=newArcNode;p=p->nextarc;p->adjvex=ch;p->nextarc=NULL;cin>>ch;}}}voidOutputALG(ALGraph*&ALG){ArcNode*p;cout<<"邻接表如下:"<n;i++){cout<adjlist[i].data<<":"
5、;p=ALG->adjlist[i].firstarc;while(p!=NULL){cout<adjvex<<"t";p=p->nextarc;}cout<>H;cout<<"请以矩阵的方式输入顶点关系:";intj=0;for(inti=0;in;i++)for(j=0;jn;j++)cin>>MG->edges[i][j];for(i=0;in;i++)for(j=0;jn;j++)
6、if(MG->edges[i][j]==1){MG->e++;}if(H=='N'){MG->e=MG->e/2;}}voidOutputMG(MGraph*&MG){intj;cout<n;i++){for(j=0;jn;j++)cout<edges[i][j]<<"t";cout<n=MG->n;ALG->e=MG-
7、>e;for(i=0;in;i++){for(j=0;jn;j++){if(MG->edges[i][j]!=0){p=newArcNode;p->adjvex=j;p->nextarc=ALG->adjlist[i].firstarc;ALG->adjlist[i].firstarc=p;}}}}voidALGtoMG(ALGraph*&ALG,MGraph*MG){inti,j;ArcNode*p;MG->n=ALG->
此文档下载收益归作者所有