已阅读5页,还剩4页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
#include#include #include #define MAX_NUM 20bool visitedMAX_NUM;/访问标致数组bool found;int fomer=0;char v1,v2;int tfind;typedef struct ArcNode int adjvex; / 该弧所指向的顶点的位置 struct ArcNode *nextarc; / 指向下一条弧的指针 / InfoType *info; / 该弧相关信息的指针 ArcNode;typedef struct VNode char data; / 顶点信息 int data2; int sngle; ArcNode *firstarc; / 指向第一条依附该顶点的弧 VNode, AdjListMAX_NUM;typedef struct AdjList vertices; int vexnum, arcnum; int kind; / 图的种类标志 ALGraph;void DFS(ALGraph G,int v);typedef struct qnode /队列类型int data;qnode *next;qnode,*queueptr;typedef struct queueptr front;queueptr rear;linkqueue;typedef struct stack/用栈存储路径char *base;char *top;int stacksize;int size;Stack;Stack s;int initstack(Stack &s)s.base=(char*)malloc(40*sizeof(char);s.top=s.base;s.stacksize=40;s.size=0;return 1;int push(Stack &s,char e) *s.top+=e; s.size+; return 1;int pop(Stack &s,char &e)if(s.base=s.top)e=*-s.top;else e=*-s.top;s.size-;return 1;void printstack(Stack s)while(s.base!=s.top)printf(%c ,*s.base);s.base+;printf(n);void printstack2(Stack s)while(s.base!=s.top)printf(%c ,*-s.top);printf(n);int intitqueue(linkqueue &q)/初始化队列 q.front=q.rear=(queueptr)malloc(sizeof(qnode);q.front-next=NULL;return 1;int emptyqueue(linkqueue q)/判断对了是否为空 if (q.front=q.rear) return 1;return 0;int enqueue(linkqueue &q,int e)/元素入队queueptr p;p=(queueptr)malloc(sizeof(qnode);if(!p) exit(0);p-data=e; p-next=NULL;q.rear-next=p;q.rear=p;return 1;int dequeue(linkqueue &q,int &e)/元素出队queueptr p;if(q.front=q.rear) return 0;p=q.front-next;e=p-data;q.front-next=p-next;if(q.rear=p) q.rear=q.front;free(p);return 1;int LocateVex(ALGraph &G,char v)int i; for(i=0;iadjvex;return -1;int NextAdjVex(ALGraph G,int v,int w)while (G.verticesv.firstarc-nextarc!=NULL)if(G.verticesv.firstarc-adjvex=w)return G.verticesv.firstarc-nextarc-adjvex;else G.verticesv.firstarc=G.verticesv.firstarc-nextarc;return -1;void Create(ALGraph &G)int i,j,k;char v1,v2;ArcNode *p,*q,*h;q=NULL;h=NULL;printf(输入节点个数和弧的个数:n); scanf(%d%d,&G.vexnum,&G.arcnum); for(i=0;iG.vexnum;i+) fflush(stdin); printf(输入节点名称:n); scanf(%c,&G.verticesi.data); G.verticesi.firstarc=NULL; G.verticesi.data2=i; for(k=0;kadjvex=j; p-nextarc=NULL; if (G.verticesi.firstarc=NULL) G.verticesi.firstarc=p; else q=G.verticesi.firstarc; while(q-nextarc!=NULL) q=q-nextarc; q-nextarc=p; void DFSTraverse(ALGraph G)/深度遍历int v; for(v=0;v=0)&(tfind=0);w=NextAdjVex(G,v,w) if(!visitedw) DFS(G,w); void DFSTree(ALGraph G)/广度遍历int w,u,v;linkqueue q;intitqueue(q);for (v=0;v0;w=NextAdjVex(G,u,w)if (!visitedw)visitedw=true;printf(%c ,G.verticesw.data);if(w0)enqueue(q,w);printf(n);void DFS2(ALGraph G,int v)/用深度遍历算法实现搜索简单路径int w;char e;visitedv=true;push(s,G.verticesv.data);for(w=FirstAdjVex(G,v);(w=0)&(!found);w=NextAdjVex(G,v,w)if(G.verticesw.data=v2)found=true;push(s,G.verticesw.data);else if(!visitedw) DFS2(G,w);if(!found) pop(s,e);void Simplepath(ALGraph G)/搜索简单路径 printf(输入要搜索路径的两点:n); fflush(stdin); scanf(%c,&v1); fflush(stdin); scanf(%c,&v2); DFS2(G,LocateVex(G,v1); if (!found) printf(can not found zhe path!n); else printstack(s);void DFSTree2(ALGraph G,int v)/用广度优先求最短路径int w,u;linkqueue q;intitqueue(q); if (!visitedv)visitedv=true;G.verticesv.sngle=-1;enqueue(q,v);while (!emptyqueue(q)dequeue(q,u);for (w=FirstAdjVex(G,u);(w0)&(!found);w=NextAdjVex(G,u,w)if (!visitedw)visitedw=true;G.verticesw.sngle=u;if(w0)enqueue(q,w);if(G.verticesw.data=v2)found=true;while (G.verticesw.sngle!=-1)push(s,G.verticesw.data);w=G.verticesw.sngle;printf(n);void shortcut(ALGraph G)/搜索最短路径 printf(输入要搜索路径的两点:n); fflush(stdin); scanf(%c,&v1); fflush(stdin); scanf(%c,&v2); DFSTree2(G,LocateVex(G,v1); push(s,v1); printstack2(s);printf(n);void main()int v;ALGraph G;found=false; initstack(s); Create(G); while(1) for(v=0;vG.vexnum;v+) visitedv=false; G.verticesv.sngle=-2; tfind=0;system(cls); printf(-n); printf(1、深度优先遍历n); printf(2、广度优先遍历n);
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 施工标志牌合同范本
- 旅行免责协议书范本
- 结构工程师设计辅助培训试题及答案
- 2026-2031年中国农产品物流行业市场全景调研与竞争格局研究报告
- 柜体定制意向合同范本
- 五金供货合同协议书
- 基于构式语法的英语句子论元结构深度解析
- 基于条件Copula模型剖析股票市场间危机传染效应
- 2026-2031年中国数控机床市场竞争现状分析与投资商机研究报告
- 上海市浦东新区2025-2026学年九年级(上)期中化学试卷(含答案)
- 2024届天一大联考高二物理第一学期期中统考试题含解析
- 「干货」12槽8极分数槽集中绕组永磁电机结构及其工作原理讲解
- 森林防火通道施工方案
- 22G101 三维彩色立体图集
- THNNJ 0001-2023 农用连栋钢架大棚技术规范
- 海龟汤题目和答案全套
- 一元线性回归分析
- 富贵包形成原因和治疗方法
- 充电桩项目工程施工设计方案
- 医疗纠纷的防范与处理智慧树知到答案章节测试2023年中南大学
- GB/T 13277.8-2023压缩空气第8部分:固体颗粒质量浓度测量方法
评论
0/150
提交评论