ImageVerifierCode 换一换
格式:DOCX , 页数:20 ,大小:136.75KB ,
资源ID:5302714      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/5302714.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(数据结构visualc++用邻接矩阵表示给定无向图并进行深度遍历.docx)为本站会员(b****4)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

数据结构visualc++用邻接矩阵表示给定无向图并进行深度遍历.docx

1、数据结构visualc+用邻接矩阵表示给定无向图并进行深度遍历1.给定无向图,请用邻接矩阵表示法表示该图 #include#includeusing namespace std;#define MAX 20typedef int AdjMAXMAX;typedef struct string vexsMAX; /顶点表 Adj arcs; /邻接矩阵 int vexnum,arcnum; /图的顶点和弧数MGraph;int LocateVex(MGraph &G,string u);int CreateUDN(MGraph &G) int i,k,j;string v1,v2; coutG.

2、vexnumG.arcnum; cout输入顶点:; for(i=0;iG.vexsi; /构造顶点数 for(i=0;iG.vexnum;i+) /构造邻接矩阵 for(j=0;jG.vexnum;j+) G.arcsij=0; for(k=0;kG.arcnum;k+) cout输入第k+1v1v2; i=LocateVex(G,v1); j=LocateVex(G,v2); G.arcsij=1; G.arcsji=1; /置的对称弧 return 0;int LocateVex(MGraph &G,string u) /确定u在G中序号 int i; for (i=0;iG.vexnu

3、m;i+) if (u=G.vexsi) return i; if (i=G.vexnum) coutError u!endl; exit(1); return 0;void ShowG(MGraph &G) int i,j; for(i=0;iG.vexnum;i+) coutG.vexsi ; coutendl; for(i=0;iG.vexnum;i+) for(j=0;jG.vexnum;j+) coutG.arcsij ; coutendl; main() MGraph A; int a; a=CreateUDN(A); ShowG(A);2.分别使用邻接矩阵表示法和邻接表表示法,用

4、深度优先搜索法遍历该图。#include# include # include # include using namespace std;int visited30;# define MAX_VERTEX_NUM 30# define OK 1/typedef int VertexType;typedef int InfoType;typedef struct ArcNode /弧 int adjvex; struct ArcNode *nextarc;ArcNode;typedef struct VNode/表头 int data; ArcNode *firstarc;VNode,AdjL

5、istMAX_VERTEX_NUM;typedef struct/图 AdjList vertices; int vexnum,arcnum; int kind;ALGraph;void CreateDG(ALGraph &G) int k,i,v1; coutendlG.vexnum; coutG.arcnum; for(i=1;i=G.vexnum;i+)/初使化表头 G.verticesi.data=i; G.verticesi.firstarc=NULL; for(k=1;k=G.vexnum;k+) /输入边 int v2; cout请输入与结点kv2; cout请输入与第kv1;

6、ArcNode *p; p=(ArcNode*)malloc(sizeof(ArcNode); if(!p) exit(-1); p-adjvex=v1; p-nextarc=NULL; G.verticesk.firstarc=p; for(int i=1;iv2;i+) int m; cout请输入与第km; ArcNode *q; q=(ArcNode *)malloc(sizeof(ArcNode);/动态指针 if(!q) exit(-1); q-adjvex=m; /顶点给P q-nextarc=NULL; p-nextarc=q; p=q; /free(q); /free(p);

7、 void DFS (ALGraph G,int v )/深度搜索 visitedv=1; coutG.verticesv.datanextarc) w=x-adjvex; if(visitedw=0) DFS(G,w); void DFSB (ALGraph G,int v)/深度搜索的边集 visitedv=1; ArcNode *y; y=(ArcNode*)malloc(sizeof(ArcNode); if(!y) exit(-1); y=G.verticesv.firstarc; int u=G.verticesv.data; int w; for(;y;y=y-nextarc)

8、w=y-adjvex; if(visitedw=0) coutuwnext=NULL;void EnQueue (LinkQueue &Q,int e)/进队 QNode *p; p=(QNode*)malloc(sizeof(QNode); if(!p) exit(-1); p-data=e; p-next=NULL; Q.rear-next=p; Q.rear=p; /free(p);int DeQueue (LinkQueue &Q,int &e)/出队 if(Q.front=Q.rear) return -1; QNode *p; p=(QNode*)malloc(sizeof(QNo

9、de); if(!p) exit(-1); p=Q.front-next; e=p-data; Q.front-next=p-next; if(Q.rear=p) Q.rear=Q.front; free(p); return e;int QueueEmpty (LinkQueue Q)/判断队列是否为空 if(Q.front=Q.rear) return 1; return 0; void BFS(ALGraph G,int v)/广度搜索 int u; LinkQueue Q; InitQueue(Q); if(visitedv=0) visitedv=1; coutG.verticesv

10、.dataadjvex;w=0;w=z-nextarc-adjvex) if(visitedw=0) visitedw=1; coutwnextarc) w=z-adjvex; if(visitedw=0) visitedw=1; coutwnextarc) w=r-adjvex; if(visitedw=0) visitedw=1; coutuwendl; EnQueue(Q,w); int main() int i; ALGraph G; CreateDG(G); int x; coutx; cout邻接表为:endl; for(int j=1;j=x;j+) coutG.vertices

11、j.data ; ArcNode *p; p=(ArcNode*)malloc(sizeof(ArcNode); if(!p) exit(-1); p=G.verticesj.firstarc; while(p) coutadjvexnextarc; coutendl; cout请输入第一个要访问的结点序号:n; for( i=0;i30;i+) visitedi=0; cout广度搜索:endl; BFS(G,n); for( i=0;i30;i+) visitedi=0; coutendl; cout边集:endl; BFSB(G,n); for( i=0;i30;i+) visitedi

12、=0; cout深度搜索:endl; DFS(G,n); for( i=0;i30;i+) visitedi=0; coutendl; cout边集:endl; DFSB(G,n); /system(pause); return 0;3.对学生选课工程图进行拓扑排序.#include#include#define MAX_VEXTEX_NUM 20#define M 20#define STACK_INIT_SIZE 100#define STACKINCREMENT 10#define OK 1#define ERROR 0typedef int ElemType;typedef struc

13、t ArcNodeint adjvex;struct ArcNode *nextarc;ArcNode;typedef struct VNodeint data;ArcNode *firstarc;VNode,AdjListMAX_VEXTEX_NUM;typedef structAdjList vertices;int vexnum, arcnum;ALGraph;typedef struct /构件栈ElemType *base;ElemType *top;int stacksize;SqStack;void InitStack(SqStack *); /函数声明int Pop(SqSta

14、ck *, ElemType *);void Push(SqStack *,ElemType );int StackEmpty(SqStack *);void CreatGraph(ALGraph *);void FindInDegree(ALGraph , int * );void TopologicalSort(ALGraph );void InitStack(SqStack *S)/初始化栈S-base=(ElemType *)malloc(STACK_INIT_SIZE*sizeof(ElemType);if(!S-base)printf(memory allocation faile

15、d, goodbye);exit(1);S-top=S-base;S-stacksize=STACK_INIT_SIZE;int Pop(SqStack *S,ElemType *e)/出栈操作if(S-top=S-base)return ERROR;*e=*-S-top;/printf(%dn,e);/ return e;return 0;void Push(SqStack *S,ElemType e)/进栈操作if(S-top-S-base=S-stacksize)S-base = (ElemType *)realloc(S-base,(S-stacksize+STACKINCREMENT

16、)*sizeof(ElemType);if(!S-base)printf(memory allocation failed, goodbye);exit(1);S-top = S-base+S-stacksize;S-stacksize+=STACKINCREMENT;*S-top+=e;int StackEmpty(SqStack *S)/判断栈是否为空if(S-top=S-base)return OK;elsereturn ERROR;void CreatGraph(ALGraph *G)/构件图int m, n, i;ArcNode *p;printf(请输入顶点数和边数:);scanf

17、(%d%d,&G-vexnum,&G-arcnum);for (i = 1; i vexnum; i+)G-verticesi.data = i;G-verticesi.firstarc = NULL;for (i = 1; i arcnum; i+) /输入存在边的点集合printf(n请输入存在边的两个顶点的序号:);scanf(%d%d,&n,&m);while (n G-vexnum | m G-vexnum)printf(输入的顶点序号不正确 请重新输入:);scanf(%d%d,&n,&m);p = (ArcNode*)malloc(sizeof(ArcNode);if (p =

18、NULL)printf(memory allocation failed,goodbey);exit(1);p-adjvex = m;p-nextarc = G-verticesn.firstarc;G-verticesn.firstarc = p;printf(建立的邻接表为:n); /输出建立好的邻接表for(i = 1; i vexnum; i+)printf(%d,G-verticesi.data);for(p = G-verticesi.firstarc; p; p = p-nextarc)printf(%3d,p-adjvex);printf(n);void FindInDegre

19、e(ALGraph G, int indegree)/求入度操作int i;for (i = 1; i = G.vexnum; i+)indegreei = 0;for (i = 1; i adjvex+;G.verticesi.firstarc = G.verticesi.firstarc-nextarc;void TopologicalSort(ALGraph G) /进行拓扑排序int indegreeM;int i, k, n;int count = 0;ArcNode *p;SqStack S;FindInDegree(G, indegree);InitStack(&S);for (i = 1; i = G.vexnum; i+)printf(第%d个点的入度为%d n, i, indegreei);printf(n);for ( i = 1; i nextarc)k = p-adjvex;if (!(-indegreek)Push(&S,k);printf(n);if (count G.vexnum)printf(出现错误n);elseprintf(排序成功n);int main(void) /主函数ALGraph G;CreatGraph(&G);TopologicalSort(G);system(pause);return 0;

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1