图的邻接表和邻接矩阵的相互转换.doc_第1页
图的邻接表和邻接矩阵的相互转换.doc_第2页
图的邻接表和邻接矩阵的相互转换.doc_第3页
图的邻接表和邻接矩阵的相互转换.doc_第4页
图的邻接表和邻接矩阵的相互转换.doc_第5页
免费预览已结束,剩余1页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

#include#include#includetypedef int InfoType;typedef int Vertex;#define MAXV 5typedef struct int no;InfoType info;VertexType;typedef struct int edgesMAXVMAXV;int n,e;VertexType vexsMAXV;MGraph;typedef struct ANode int adjvex;struct ANode *nextarc;InfoType info;ArcNode;typedef struct Vnode Vertex data;ArcNode *firstarc;VNode;typedef VNode AdjListMAXV;typedef struct AdjList adjlist;int n,e;ALGraph;void InitialMG(MGraph *&MG) MG=new MGraph;MG-e=0;MG-n=5;int j;for(int i=0;ivexsi.no=i;for(i=0;iMAXV;i+)for(j=0;jedgesij=0;void InitialALG(ALGraph *&ALG) ALG=new ALGraph;ALG-n=5;ALG-e=0;for(int i=0;iadjlisti.data=i; ALG-adjlisti.firstarc=NULL;void InputALG(ALGraph *&ALG) int ch;ArcNode *p;coutendl请输入邻接表,以-1结束输入(形式如:4 :5 5 6 -1):endl;for(int i=0;in;i+) coutadjlisti.datach;if(ch!=-1) ALG-adjlisti.firstarc=new ArcNode;p=ALG-adjlisti.firstarc;p-adjvex=ch;p-nextarc=NULL; cinch;while(ch!=-1)p-nextarc=new ArcNode;p=p-nextarc;p-adjvex=ch;p-nextarc=NULL;cinch;void OutputALG(ALGraph *&ALG) ArcNode *p;cout邻接表如下:endl;for(int i=0;in;i+)coutadjlisti.dataadjlisti.firstarc;while(p!=NULL) coutadjvexnextarc;coutendl;void InputMG(MGraph *&MG) char H;cout请输入(H/N)(有向/无向):H;cout 请以矩阵的方式输入顶点关系:n;int j=0;for(int i=0;in;i+)for(j=0;jn;j+) cinMG-edgesij;for(i=0;in;i+)for(j=0;jn;j+)if(MG-edgesij=1) MG-e+;if(H=N) MG-e=MG-e/2;void OutputMG(MGraph *&MG) int j;coutendl图的邻接矩阵如下:endl;for(int i=0;in;i+)for(j=0;jn;j+)coutedgesijt;coutendl;coutn=MG-n;ALG-e=MG-e;for(i=0;in;i+) for(j=0;jn;j+) if(MG-edgesij!=0) p=new ArcNode;p-adjvex=j;p-nextarc=ALG-adjlisti.firstarc;ALG-adjlisti.firstarc=p;void ALGtoMG(ALGraph *&ALG,MGraph *MG) int i,j;ArcNode *p;MG-n=ALG-n;for(i=0;in;i+) if(ALG-adjlisti.firstarc!=NULL) p=ALG-adjlisti.firstarc;while(p!=NULL) MG-edgesip-adjvex=1;p=p-nextarc;for(i=0;in;i+) for(j=0;jn;j+) if(MG-edgesij!=1) MG-edgesij=0;void MatToList(MGraph g,ALGraph *&G) /将临街矩阵g转换成邻接表G int i,j;ArcNode *p;G=(ALGraph *)malloc(sizeof(ALGraph);for(i=0;iadjlisti.firstarc=NULL;for(i=0;i=0;j-)if(g.edgesij!=0) p=(ArcNode *)malloc(sizeof(ArcNode);p-adjvex=j;p-nextarc=G-adjlisti.firstarc;G-adjlisti.firstarc=p;G-n=g.n;G-e=g.e;void ListToMat(ALGraph *G,MGraph &g) /将邻接表G转换为邻接矩阵g int i;ArcNode *p;for(i=0;in;i+) p=G-adjlisti.firstarc;while(p!=NULL) g.edgesip-adjvex=1;p=p-nextarc;g.n=G-n;g.e=G-e;void main() / MGraph *MG;ALGraph *ALG;InitialALG(ALG);/InitialMG(MG);/InputMG(MG);/OutputMG(MG); InputALG(ALG); OutputALG(ALG);/Transchange(MG,ALG);/OutputALG(ALG);/

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论