已阅读5页,还剩2页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
数据结构实验题目:实现教材P215例8.3的完整算法(判断无向图是否联通)源码:#include malloc.h#include #include #define MAXV 6#define InfoType int#define Vertex inttypedef structint no;InfoType info; VertexType;typedef structint edgeMAXVMAXV;int n,e;VertexType vexsMAXV;MGraph;typedef struct ANodeint adjvex;struct ANode *nextarc;InfoType info;ArcNode;typedef struct VNodeVertex data;ANode *firstarc;VNode;typedef VNode AdjListMAXV;typedef structAdjList adjlist;int n,e;ALGraph;int visitedMAXV=0,0,0,0,0,0;void ListToMat(MGraph g,ALGraph * &G)int i,j;ArcNode *p;for(i=0;in;i+)for(j=0;jn;j+)g.edgeij=0;for(i=0;iadjlisti.firstarc;while(p!=NULL)g.edgeip-adjvex=1;p=p-nextarc;g.n=G-n;g.e=G-e;void BFS(ALGraph *G,int v)ArcNode *p;int i,j;visitedv=1;printf(%d,v);p=G-adjlistv.firstarc;while(p!=NULL) printf(%d,p-adjvex);if(visitedp-adjvex!=1)BFS(G,p-adjvex);p=p-nextarc;void BFSbook(ALGraph *G,int v)ArcNode *p;int queueMAXV,front=0,rear=0; int w ,i;printf(%2d,v);visitedv=1;rear=(rear+1)%MAXV;queuerear=v;while(front!=rear)front=(front+1)%MAXV;w=queuefront;p=G-adjlistw.firstarc;while(p!=NULL)if(visitedp-adjvex=0)printf(%2d,p-adjvex);visitedp-adjvex=1;rear=(rear+1)%MAXV;queuerear=p-adjvex;p=p-nextarc;printf(n);void DFS(ALGraph *G,int v)coutDFSnadjlistv.firstarc;visitedv=1;printf(=%d= n,v);while(p!=NULL) printf(|%d| n,p-adjvex);if(visitedp-adjvex=0)DFS(G,p-adjvex);p=p-nextarc; bool connect(ALGraph * G)int i;bool flag=true;for(i=0;in;i+)visitedi=0;BFSbook(G,3); coutDispVisitedendl;for(i=0;in;i+)coutvisitediendl;for(i=0;in;i+)coutntiendl;if(visitedi=0)flag=false;break;coutnnendl;return flag;void MatToList(MGraph g,ALGraph * &G) int i,j,k=0,flag=1;ArcNode * p;G=(ALGraph *)malloc(sizeof(ALGraph);for(i=0;iadjlisti.firstarc=NULL;for(i=0;i=0;j-)if(g.edgeij=1)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 DispGraph(ALGraph * &G,int num)int i;ArcNode *p;for(i=0;i,i);p=G-adjlisti.firstarc;while(p!=NULL)printf(%d-,p-adjvex);p=p-nextarc;printf(n);void DispMat(MGraph g)int i,j;for(i=0;ig.n;i+)for(j=0;jg.n;j+)printf(%dt,g.edgeij);printf(n);MGraph DataToMat(MGraph mgraph,int dataMAXVMAXV)int i,j;for(i=0;iMAXV;i+)for(j=0;jMAXV;j+)mgraph.edgeij=dataij;mgraph.n=MAXV;mgraph.e=14;return mgraph;int main()/矩阵图数据源int dataMAXVMAXV=0,1,0,1,0,1,1,0,1,0,0,0,0,1,0,0,1,0,1,0,0,0,1,0,0,0,1,1,0,1,1,0,0,0,1,0;ALGraph * algraph;MGraph mgraph;mgraph=DataToMat(mgraph,data);DispMat(mgraph);MatToList(mgraph,algraph);DispGraph(algraph,MAXV);cout是否连通:(connect
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 国企招聘考试面试学员专用讲义结构化2-考题精讲-第三场
- 荆门市重点中学2024年中考语文猜题卷含解析
- 江西省宜春市第九中学2023-2024学年中考三模数学试题含解析
- 江西省上饶市四中重点中学2024届中考数学仿真试卷含解析
- 江西省萍乡市莲花县2023-2024学年中考三模物理试题含解析
- 私人停车位使用权转让合同合集
- 向法院申请小孩抚养费申请书合集
- 个人装修合同书范本合集
- 交通肇事的逃逸协议书范文合集
- 广告安装安全协议书合集
- 生物制药技术与工程课件
- 2022-2023学年广东省深圳市南山区小升初数学模拟试卷含答案
- 精细化管理促进安全管理再上新台阶
- 摄影实操技能理论考试复习题库(含答案)
- 供电所的机构设置及职责
- 2023年上海社会科学院工作人员招聘笔试备考试题及答案解析
- 集中空调预防空气传播性疾病应急预案
- 高考英语应用文写作之科技篇
- M1152-P280GH钢无缝碳钢钢管RCCM中文版法国民用核电标准
- 环氧氯丙烷安全技术说明书
- 高中物理人教版(2019)选择性必修一 第三章《机械波》课后习题练习与应用复习与提高 含答案
评论
0/150
提交评论