长江大学数据结构程序设计报告文档格式.docx
《长江大学数据结构程序设计报告文档格式.docx》由会员分享,可在线阅读,更多相关《长江大学数据结构程序设计报告文档格式.docx(53页珍藏版)》请在冰豆网上搜索。
![长江大学数据结构程序设计报告文档格式.docx](https://file1.bdocx.com/fileroot1/2022-12/30/4456729f-c556-42e1-9174-78db2caaf8b0/4456729f-c556-42e1-9174-78db2caaf8b01.gif)
4单源最短路径
5每对顶点之间的最短路径
三、设计方案
DOS界面的主菜单
voidShowMainMenu()
{
cout<
<
"
\n"
;
*****************图的基本操作及应用****************\n"
*1无向图的基本操作及应用*\n"
*2无向网的基本操作及应用*\n"
*3有向图的基本操作及应用*\n"
*4有向网的基本操作及应用*\n"
*5退出*\n"
***************************************************\n"
}
voidUDG1()
do
{
cout<
****************无向图的基本操作及应用*************\n"
*1创建无向图的邻接矩阵*\n"
*2创建无向图的邻接表*\n"
*3无向图的深度优先遍历*\n"
*4无向图的广度优先遍历*\n"
cin>
>
n;
switch(n)
{
case1:
创建无向图的邻接矩阵
break;
case2:
创建无向图的邻接表
case3:
无向图的深度优先遍历
case4:
无向图的广度优先遍历
break;
default:
if(n!
=5)
cout<
错误,重新输入\n"
}
}while(n!
=5);
voidUDN1()
do{
***************无向网的基本操作及应用**************\n"
*1创建无向网的邻接矩阵*\n"
*2创建无向网的邻接表*\n"
*3最小生成树*\n"
*4退出*\n"
case1:
创建无向网的邻接矩阵
break;
case2:
创建无向网的邻接表
case3:
最小生成树
default:
if(n!
=4)
cout<
错误,重新输入\n"
=4);
voidDG1()
***************有向图的基本操作及应用**************\n"
*1创建有向图的邻接表*\n"
*2创建有向图的邻接矩阵*\n"
*3拓扑排序*\n"
创建有向图的邻接表
创建有向图的邻接矩阵
拓扑排序
voidDN1()
***************有向网的基本操作及应用**************\n"
*1创建有向网的邻接矩阵*\n"
*2创建有向网的邻接表*\n"
*3关键路径*\n"
*4单源最短路径*\n"
*5每对顶点间的最短路径*\n"
*6退出*\n"
创建有向网的邻接矩阵
创建有向网的邻接表
关键路径
case4:
单源最短路径
case5:
每对顶点间的最短路径
print1(MK);
=6)
=6);
voidmain()
intn;
ShowMainMenu();
UDG1();
UDN1();
DG1();
DN1();
cout<
错误,重新输入"
}
四、实现代码:
#include<
iostream>
stdlib.h>
iomanip>
stdio.h>
stack>
usingnamespacestd;
#defineMAX_VERTEX_NUM100//最大顶点个数
#defineINFINITY10000//最大值
#defineFALSE0
#defineTRUE1
typedefintVRType;
//顶点关系(表示是否相邻)
typedefcharVertexType;
typedefintInfoType;
//弧相关信息
typedefenum{DG,DN,UDG,UDN}GraphKind;
//{有向图,有向网,无向图,无向网}
typedefstructArcCell
VRTypeadj;
//权值
InfoType*info;
//该弧相关信息的指针
}ArcCell,AdjMartix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
typedefstruct
VertexTypevexs[MAX_VERTEX_NUM];
//顶点向量
AdjMartixarcs;
//邻接矩阵
intvexnum,arcnum;
//图当前顶点数,弧数
GraphKindKind;
//图的类型
}MGraph;
intLocateVex(MGraphG,VertexTypev)//若图中存在v,返回v在图中的位置
for(inti=0;
i<
G.vexnum;
i++)
if(v==G.vexs[i])
returni;
}
return-2;
//***********************创建无向网的邻接矩阵**********************
intCreatMUDN(MGraph&
G)//构造无向网
inti,j,w;
charch;
VertexTypev1,v2;
输入顶点数,弧数:
endl;
cin>
G.vexnum>
G.arcnum;
//输入当前顶点数弧数是否有弧信息
输入顶点(字符型):
for(i=0;
i++)//初始化邻接矩阵
for(j=0;
j<
j++)
G.arcs[i][j].adj=INFINITY;
G.arcs[i][j].info=NULL;
i++)//顶点信息
输入第"
i+1<
个顶点:
ch;
G.vexs[i]=ch;
for(intk=0;
k<
k++)
k+1<
条弧:
v1>
v2;
输入弧的权值:
w;
if((i=LocateVex(G,v1))!
=-2)//
if((j=LocateVex(G,v2))!
=-2)
G.arcs[i][j].adj=w;
//对弧写入权值
G.arcs[j][i].adj=w;
//对称弧赋值
return1;
//***********************创建无向图的邻接矩阵**********************
voidCreatUDG(MGraph&
G)
inti,j,k;
G.vexnum>
for(j=0;
G.arcs[i][j].adj=0;
G.arcs[i][j].info=NULL;
for(k=0;
k++)
if((i=LocateVex(G,v1))!
G.arcs[i][j].adj=1;
G.arcs[j][i].adj=1;
//***********************显示邻接矩阵的信息**********************
voidScanAll(MGraphG)
inti;
图中顶点信息如下:
G.vexs[i]<
"
邻接矩阵如下:
setw(5)<
矩阵:
setw(8)<
G.vexs[i];
setw(6)<
for(intj=0;
G.arcs[i][j].adj;
typedefstructArcNode{
intadjvex;
//该弧指向顶点的位置
structArcNode*nextarc;
//指向下一条弧的指针
}ArcNode;
typedefstructVNode
VertexTypedata;
//顶点信息
ArcNode*firstarc;
//指向第一条依附该顶点弧的指针
}VNode,AdjList[MAX_VERTEX_NUM];
AdjListvertices;
intkind;
}ALGraph;
//************************创建无向图的邻接表**************************
voidCreatALGUDG(ALGraph&
inti,s,d;
ArcNode*p,*q;
输入图的顶点数和边数:
G.arcnum;
for(i=1;
=G.vexnum;
\n输入第"
个顶点的信息:
G.vertices[i].data;
G.vertices[i].firstarc=NULL;
=G.arcnum;
条边的头结点和尾结点(整数):
s>
d;
p=newArcNode;
p->
adjvex=d;
nextarc=G.vertices[s].firstarc;
G.vertices[s].firstarc=p;
//将新建的以d为信息的表结点p插入s单链表的头结点后
q=newArcNode;
q->
adjvex=s;
nextarc=G.vertices[d].firstarc;
G.vertices[d].firstarc=q;
G.kind=UDG;
//***********************输出邻接表**********************
voidPrintALGUDG(ALGraphG)
ArcNode*p;
=G.vexnum;
p=G.vertices[i].firstarc;
while(p!
=NULL)
G.vertices[i].data<
"
G.vertices[p->
adjvex].data<
p=p->
nextarc;
printf("
);
//****************************无向图的深度遍历******************************
intvisited[MAX_VERTEX_NUM];
voidDFS(ALGraphG,intv)
G.vertices[v].data;
visited[v]=1;
p=G.vertices[v].firstarc;
while(p!
if(visited[p->
adjvex]==0)
DFS(G,p->
adjvex);
p=p->
voidDFSTraverse(ALGraphG)
intv;
for(v=1;
v<
v++)
visited[v]=0;
if(visited[v]==0)
DFS(G,v);
//****************************无向图的广度遍历******************************
intFirstAdjvex(ALGraphG,intv)
if(p=G.vertices[v].firstarc)
returnp->
adjvex;
return0;
intNextAdjVex(ALGraphG,intv,intw)
while(p->
adjvex!
=w)
nextarc;
if(p->
nextarc)
nextarc->
else
return0;
typedefstructQnode
intdata;
structQnode*next;
}Qnode;
Qnode*front;
Qnode*rear;
}Linkqueue;
intInitqueue(Linkqueue*Q)
Q->
front=(Qnode*)malloc(sizeof(Qnode));
rear=Q->
front;
if(!
Q->
front)
front->
next=NULL;
intEnqueue(Linkqueue*Q,int*e)
Qnode*p;
p=(Qnode*)malloc(sizeof(Qnode));
p)
p->
data=*e;
rear->
next=p;
rear=p;
voidDequeue(Linkqueue*Q,int*e)
if(Q->
front==Q->
rear)
空"
p=Q->
next;
*e=p->
data;
next=p->
rear==p)
Q->
free(p);
voidBFSTraverse(ALGraphG)
intv,w,u;
++v)
LinkqueueQ;
Initqueue(&
Q);
if(!
visited[v])
visited[v]=1;
cout<
Enqueue(&
Q,&
v);
while(Q.front!
=Q.rear)
{
Dequeue(&
u);
for(w=FirstAdjvex(G,u);
w>
=1;
w=NextAdjVex(G,u,w))
if(!
visited[w])
{
visited[w]=1;
cout<
G.vertices[w].data;
Enqueue(&
w);
}
}
//*********************************最小生成树***************************
voidMiniSpanTree(MGraphG,VertexTypeu)
intk;
intj,i;
intcount=0,min;
struct
VertexTypeadjvex;
intlowcost;
}closedge[MAX_VERTEX_NUM];
k=LocateVex(G,u);
if(j!
=k)
closedge[j].adjvex=u;
closedge[j].lowcost=G.arcs[k][j].adj;
};
closedge[k].lowcost=0;
count=1;
最小生成树的各个边信息如下:
while(count!
=G.vexnum)
min=1000;
if(closedge[i].lowcost==INFINITY||c