资源预览内容
第1页 / 共9页
第2页 / 共9页
第3页 / 共9页
第4页 / 共9页
第5页 / 共9页
第6页 / 共9页
第7页 / 共9页
第8页 / 共9页
第9页 / 共9页
亲,该文档总共9页全部预览完了,如果喜欢就下载吧!
资源描述
数 据 结 构实验报告 目的要求 掌握图的存储思想及其存储实现。 掌握图的深度、广度优先遍历算法思想及其程序实现。 掌握图的常见应用算法的思想及其程序实现。实验容键盘输入数据,建立一个有向图的邻接表。 输出该邻接表。 3在有向图的邻接表的基础上计算各顶点的度,并输出。 4以有向图的邻接表为基础实现输出它的拓扑排序序列。 5采用邻接表存储实现无向图的深度优先递归遍历。6采用邻接表存储实现无向图的广度优先遍历。7在主函数中设计一个简单的菜单,分别调试上述算法。源程序: 主程序的头文件:队列#include #include #define TRUE 1#define FALSE 0#define OK 1#define ERROR 0#define OVERFLOW -2typedef int QElemType;typedef struct QNode /队的操作 QElemType data; struct QNode *next; QNode,*QueuePtr; typedef struct QueuePtr front; QueuePtr rear; LinkQueue;void InitQueue(LinkQueue &Q) /初始化队列 Q.front =Q.rear =(QueuePtr)malloc(sizeof(QNode); if(!Q.front) exit(OVERFLOW); /存储分配失败 Q.front -next =NULL; int EnQueue(LinkQueue &Q,QElemType e) /插入元素e为Q的新的队尾元素QueuePtr p;p=(QueuePtr)malloc(sizeof(QNode);if(!p) exit(OVERFLOW);p-data=e;p-next=NULL; Q.rear-next=p;Q.rear =p;return OK;int DeQueue(LinkQueue &Q,QElemType &e) /删除Q的队头元素,用e返回其值if(Q.front =Q.rear ) return ERROR;QueuePtr p;p=Q.front -next;e=p-data; Q.front-next=p-next ;if(Q.rear=p) Q.rear =Q.front ;free(p);return OK;主程序:#include #include#includeduilie.h#define TRUE 1#define FALSE 0#define Status int#define MAX_VERTEX_NUM 8 /*顶点最大个数*/#define VertexType char /*顶点元素类型*/enum BOOlean False,True;BOOlean visitedMAX_VERTEX_NUM; /全局变量访问标志数组 typedef struct ArcNode int adjvex; struct ArcNode *nextarc; int weight; /*边的权*/ ArcNode; /*表结点*/ typedef struct VNode int degree,indegree;/*顶点的度,入度*/ VertexType data; ArcNode *firstarc; VNode/*头结点*/,AdjListMAX_VERTEX_NUM; typedef struct AdjList vertices; int vexnum,arcnum;/*顶点的实际数,边的实际数*/ ALGraph; /建立图的邻接表 void creat_link(ALGraph *G) int i,j; ArcNode *s; printf(请依次输入顶点数、边数:); scanf(%d%d,&G-vexnum,&G-arcnum); for (i=0;ivexnum;i+)G-verticesi.data=A+i; G-verticesi.firstarc=NULL; for (i=0;ivexnum;) printf(请输入顶点的数组坐标(若退出,请输入-1):); scanf(%d,&i); if(i=-1) break; printf(请输入顶点所指向下一个顶点的数组坐标:); scanf(%d,&j); s=(ArcNode *)malloc(sizeof(ArcNode); s-adjvex=j; s-nextarc=G-verticesi.firstarc; G-verticesi.firstarc=s; / 输出邻接表 void visit(ALGraph G) int i; ArcNode *p; printf(%4s%6s%18sn,NO,data,adjvexs of arcs); for (i=0;inextarc) printf(%3d,p-adjvex); printf(n); / 计算各顶点的度及入度 void cacu(ALGraph *G) ArcNode *p; int i; for (i=0;ivexnum;i+) G-verticesi.degree=0;G-verticesi.indegree=0;/度与初度初始化为零 for (i=0;ivexnum;i+) for(p=G-verticesi.firstarc;p;p=p-nextarc) G-verticesi.degree+; G-verticesp-adjvex.degree+; G-verticesp-adjvex.indegree+; void print_degree(ALGraph G) int i; printf(n Nom data degree indegreen); for (i=0;iG.vexnum;i+) printf(n%4d%5c%7d%8d,i,G.verticesi.data, G.verticesi.degree,G.verticesi.indegree); printf(n); / 拓扑排序 Status TopologiSort(ALGraph G) int i,count,top=0,stack50; ArcNode *p; cacu(&G); print_degree(G); printf(nTopologiSort is n); for(i=0;i%c,G.verticesi.data); count+; for(p=G.verticesi.firstarc;p;p=p-nextarc) if (!-G.verticesp-adjvex.indegree)stacktop+=p-adjvex; if (countadjvex);/在图G中寻找第v个顶点的相对于u的下一个邻接顶点int NextAdjVex(ALGraph G,int v,int u) ArcNode *p; p=G.verticesv.firstarc; while(p-adjvex!=u) p=p-nextarc; /在顶点v的弧链中找到顶点u if(p-nextarc=NULL) return 0; /若已是最后一个顶点,返回0 else return(p-nextarc-adjvex); /返回下一个邻接顶点的序号/采用邻接表存储实现无向图的深度优先递归遍历void DFS(ALGraph G,int i) int w; visitedi=True; /访问第i个顶点 printf(%d-,i); for(w=FirstAdjVex(G,i);w;w=NextAdjVex(G,i,w) if(!visitedw) DFS(G,w); /对尚未访问的邻接顶点w调用DFSvoid DFSTraverse(ALGraph G) int i; printf(DFSTraverse:); for(i=0;iG.vexnum;i+) visitedi=False; /访问标志数组初始化 for(i=0;iG.vexnum;i+) if(!visitedi) DFS(G,i); /对尚未访问的顶点调用DFS/按广度优先非递归的遍历图G,使用辅助队列Q和访问标志数组visitedvoid BFSTraverse(ALGraph G) int i,u,w;
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号