免费预览已结束,剩余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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年模块电源测试题及答案
- 2026年童年河测试题加上答案
- 2026年比内智力测试题目及答案
- 阅读专项闯关题及答案
- 秘书专业试卷试题及答案分享
- 补语专项试题及详细答案
- 2026年中国天然原油产业现状深度调研及十五五盈利空间评估报告
- vte防治护理理论知识题库及答案详解
- 初学者ctf题库及答案详解
- 2026年中国快消品电商产业深度调研与发展战略研究报告
- 2026年非高危行业安全员培训考试试题含答案
- 2025年一级消防工程师继续教育考题及答案
- 2026年江苏泰州兴化市事业单位公开招聘113人易考易错模拟试题(共500题)试卷后附参考答案
- 2026年事业编行政执法类(综合执法)法律基础知识培训试卷
- 胃肠护理中的口腔卫生与胃肠健康
- 安徽省环境监测服务收费标准
- 2026-2030中国鸵鸟养殖行业市场发展分析及竞争格局与投资前景研究报告
- 湖南绿色校园建设方案
- 2026年医疗科技康复机器人技术标准报告
- 2026年经济师考试初级经济法模拟单套试卷及解析
- 水塘如何整治工作方案
评论
0/150
提交评论