数据结构图的操作.docx
《数据结构图的操作.docx》由会员分享,可在线阅读,更多相关《数据结构图的操作.docx(16页珍藏版)》请在第壹文秘上搜索。
1、实验7:图的操作算法一、实验目的1 .熟悉各种图的存储结构。2 .掌握图的各种搜索路径的遍历方法。二、实验内容L设计一个有向图和一个无向图,任选一种存储结构,完成有向图和无向图的DFS(深度优先遍历)和BFS(广度优先遍历)的操作。算法:#include#include#include#include#defineMAXV10#defineINF980708usingnamespacestd;typedefintInfoType;邻接矩阵typedefstruct(intno;InfoTypeinfo;JVertexType;顶点类型typedefstruct(intedgesMAXVMAXV
2、;intnze;VertexTypevesMAXV;MatGraph;完整的图邻接矩阵类型邻接表typedefstructANode(intadjve;structANode*nextarc;intweight;ArCNOde;边结点类型typedefstructVnodeInfoTypeinfo;ArcNode*firstarc;VNode;邻接表的头结点类型typedefstruct(VNodeadjlistMAXV;intn,e;AdjGraph;完整的图邻接表类型typedefstruct(InfoTypedataMAXV;intfront,rear;SqQueue;队列初始化队列vo
3、idInitQUeUe(SqQUeUe*&q)(q=(SqQueue*)malloc(sizeof(SqQueue);q-front=q-rear=-l;)判断队列是否为空boolQueueEmptyfSqQueue*q)(return(q-front=q-rear);)进队列booleQueue(SqQueue*&q,InfoTypee)(if(q-rear=MAXV)returnfalse;q-rear+;q-dataq-rear=e;returntrue;)出队列booldeQueue(SqQueue*&q,InfoType&e)(if(q-front=q-rear)returnfals
4、e;q-front=(q-front+l)%MAXV;e=q-dataq-front;returntrue;)创建邻接表voidCreateAdj(AdjGraph*&GJntAMAXVMAXVJntnjnte)(intij;ArcNode*p;G=(AdjGraph*)malloc(sizeof(AdjGraph);for(i=0;iadjlisti.firstarc=NULL;for(i=0;i=OJ-)if(Aij!=O&Ai0!=INF)p=(ArcNode*)malloc(sizeof(ArcNode);p-adjvex=j;p-nextarc=G-adjIisti.firstarc
5、;G-adjlisti.firstarc=p;)G-n=n;G-e=e;)输出邻接表voidDispAdj(AdjGraph*G)(COUt“邻接表存储:endl;inti;ArcNode*p;for(i=0;in;i+)(p=G-adjlisti.firstarc;printf(%3dzi);printf(%3d-,J);while(p!=NULL)(printf(%3d-,p-adjvex);p=p-nextarc;)coutAendl;)/DFS深度优先遍历intvisitedMAXV=0;voidDFSfAdjGraph*G,intv)(ArcNode*p;visitedv=l;cou
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据 结构图 操作
