数据结构图实验报告汇总Word文件下载.docx
- 文档编号:13738850
- 上传时间:2022-10-13
- 格式:DOCX
- 页数:27
- 大小:76.63KB
数据结构图实验报告汇总Word文件下载.docx
《数据结构图实验报告汇总Word文件下载.docx》由会员分享,可在线阅读,更多相关《数据结构图实验报告汇总Word文件下载.docx(27页珍藏版)》请在冰豆网上搜索。
②输出图1所示的有向图G从顶点0开始的深度优先遍历序列(非递归算法);
③输出图1所示的有向图G从顶点0开始的广度优先遍历序列。
3、设计一个程序exp8-3.cpp,采用邻接表存储图,并输出图8.1(a)中从指定顶点1出发的所有深度优先遍历序列。
(2)实验方法:
1、综合运用课本所学的知识,用不同的算法实现在不同的程序功能。
2、结合指导老师的指导,解决程序中的问题,正确解决实际中存在的异常情况,逐步改善功能。
3、根据实验内容,编译程序。
三、实验环境:
Windows7,VisualC++6.0
三、实验过程描述
文件graph.h中定义了图的邻接矩阵表示类型和邻接表表示类型,该头文件在以下三个实验中都会使用到。
其代码如下:
VertexTypevexs[MAXV];
}MGraph;
//以下定义邻接表类型
typedefstructANode
{
intadjvex;
structANode*nextarc;
InfoTypeinfo;
}ArcNode;
typedefintVertex;
typedefstructVNode
Vertexdata;
ArcNode*firstarc;
}VNode;
typedefVNodeAdjList[MAXV];
typedefstruct
AdjListadjlist;
intn,e;
}ALGraph;
#endif//GRAPH_H_INCLUDED
实验①
源程序。
一、输入如下所示程序;
//文件名:
exp8-1.cpp
#include<
stdio.h>
malloc.h>
#include"
graph.h"
externvoidMatToList1(MGraph,ALGraph*&
);
externvoidListToMat1(ALGraph*,MGraph&
externvoidDispMat1(MGraph);
externvoidDispAdj1(ALGraph*);
intmain()
inti,j;
MGraphg,g1;
ALGraph*G;
intA[MAXV][6]={{0,5,INF,7,INF,INF},{INF,0,4,INF,INF,INF},
{8,INF,0,INF,INF,9},{INF,INF,5,0,INF,6},
{INF,INF,INF,5,0,INF},{3,INF,INF,INF,1,0}};
g.n=6;
g.e=10;
for(i=0;
i<
g.n;
i++)
for(j=0;
j<
j++)
g.edges[i][j]=A[i][j];
printf("
有向图G的邻接矩阵:
\n"
DispMat1(g);
G=(ALGraph*)malloc(sizeof(ALGraph));
图G的邻接矩阵转换成邻接表:
MatToList1(g,G);
DispAdj1(G);
图G的邻接表转换成邻接矩阵:
ListToMat1(G,g1);
DispMat1(g1);
return0;
}
algo8-1.cpp
//不带权图的算法
voidMatToList(MGraphg,ALGraph*&
G)
ArcNode*p;
for(j=g.n-1;
j>
=0;
j--)
if(g.edges[i][j]!
=0)
{
p=(ArcNode*)malloc(sizeof(ArcNode));
p->
adjvex=j;
nextarc=G->
adjlist[i].firstarc;
G->
adjlist[i].firstarc=p;
}
n=g.n;
e=g.e;
voidListToMat(ALGraph*G,MGraph&
g)
G->
n;
g.edges[i][j]=0;
p=G->
while(p!
=NULL)
g.edges[i][p->
adjvex]=1;
p=p->
nextarc;
g.n=G->
g.e=G->
e;
voidDispMat(MGraphg)
%3d"
g.edges[i][j]);
voidDispAdj(ALGraph*G)
inti;
%3d:
"
i);
p->
adjvex);
//带权图的算法
voidMatToList1(MGraphg,ALGraph*&
i++)
adjlist[i].firstarc=NULL;
for(j=g.n-1;
=0&
&
g.edges[i][j]!
=INF)
info=g.edges[i][j];
voidListToMat1(ALGraph*G,MGraph&
if(i==j)
else
g.edges[i][j]=INF;
adjvex]=p->
info;
voidDispMat1(MGraphg)
if(g.edges[i][j]==INF)
%3s"
"
∞"
voidDispAdj1(ALGraph*G)
%3d(%d)"
adjvex,p->
info);
二、编译并链接程序;
三、运行程序,结果如下图:
实验
源程序
exp8-2.cpp
externvoidMatToList1(MGraph,ALGraph*&
externvoidDispAdj1(ALGraph*G);
externvoidDFS(ALGraph*G,intv);
externvoidDFS1(ALGraph*G,intv);
externvoidDFS2(ALGraph*G,intv);
externvoidBFS(ALGraph*G,intv);
MGraphg;
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据 结构图 实验 报告 汇总
![提示](https://static.bdocx.com/images/bang_tan.gif)