数据结构邻接矩阵邻接表图实验报告.docx
《数据结构邻接矩阵邻接表图实验报告.docx》由会员分享,可在线阅读,更多相关《数据结构邻接矩阵邻接表图实验报告.docx(17页珍藏版)》请在冰豆网上搜索。
![数据结构邻接矩阵邻接表图实验报告.docx](https://file1.bdocx.com/fileroot1/2023-1/25/e0592741-f47f-4721-898a-056209112610/e0592741-f47f-4721-898a-0562091126101.gif)
数据结构邻接矩阵邻接表图实验报告
实验名称:
数据结构实验五
实验内容:
1.使用邻接矩阵建立一个图,深度遍历。
2.使用邻接表建立一个图,广度遍历。
3.建立一个图,存储结构自己确定,并进行拓扑排序。
实验代码:
1.#include"stdio.h"
#defineInfinity100
#defineMaxVertexNum20
typedefenum{DG,DN,UDG,UDN}GraphKind;
typedefintVRType;
typedefcharVertexType;
boolVisit[MaxVertexNum];
typedefstructArcCell
{
VRTypeadj;
}ArcCell,AdjMatrix[MaxVertexNum][MaxVertexNum];
typedefstruct
{
VertexTypevexs[MaxVertexNum];
AdjMatrixarcs;//邻接矩阵
intvexnum,arcnum;//图的当前顶点数和弧数
GraphKindkind;
}MGraph;
intLocateVex(MGraphG,VertexTypev)
{
for(inti=0;i{
if(v==G.vexs[i])
returni;
}
if(i=G.vexnum)
printf("输入的顶点不合法\n");
return0;
}
VertexTypev1,v2;
VRTypew;
voidCreateUDG(MGraph&G)
{
inti,j,k;
printf("请输入顶点数:
\n");
scanf("%d",&G.vexnum);
printf("请输入弧数:
\n");
scanf("%d",&G.arcnum);
i=0;
while(i{
printf("请输入第%d个顶点\n",i);
getchar();
scanf("%c",&G.vexs[i]);
++i;
}
for(i=0;i{
for(j=0;jG.arcs[i][j].adj=0;
}
for(k=0;k{
printf("请输入一条边依附的顶点及权值(v1v2w)\n");
getchar();
scanf("%c%c%d",&v1,&v2,&w);
i=LocateVex(G,v1);
j=LocateVex(G,v2);
G.arcs[i][j].adj=w;
G.arcs[j][i]=G.arcs[i][j];
}
return;
}
voidDFSTraverse(MGraph&G,inti)
{
printf("%c",G.vexs[i]);
Visit[i]=true;
for(intj=0;j{
if(G.arcs[i][j].adj==1&&!
Visit[j])
{
DFSTraverse(G,j);
}
}
}
voidDFS(MGraph&G)
{
inti;
for(i=0;ifor(i=0;i{
if(!
Visit[i])
{
DFSTraverse(G,i);
}
}
}
voidmain()
{
MGraphgraph;
CreateUDG(graph);
printf("顶点集合为:
:
");
for(inti=0;iprintf("%c",graph.vexs[i]);
printf("\n深度遍历结果是:
");
DFS(graph);
printf("\n");
return;
}
2.
#include"stdio.h"
#include"stdlib.h"
#defineMaxVertexNum20
typedefintInfoType;
typedefcharVertexType;
typedefVertexTypeQElemType;
boolvisited[MaxVertexNum];
typedefstructArcNode
{
intadjvex;//该弧指向的顶点位置
structArcNode*nextarc;//指向下一条弧的指针
InfoType*info;
}ArcNode;
typedefstructVNode
{
VertexTypedata;//顶点信息
ArcNode*firstarc;//指向第一条依附该顶点的弧的指针
}VNode,AdjList[MaxVertexNum];
typedefstruct
{
AdjListvertices;
intvexnum,arcnum;//图的当前顶点数和弧数
}ALGraph;
typedefstructQNode
{
QElemTypedata;
structQNode*next;
}QNode,*Queueptr;
typedefstruct
{
Queueptrfront;
Queueptrrear;
}LinkQueue;
voidInitQueue(LinkQueue&Q)
{
Q.front=Q.rear=(Queueptr)malloc(sizeof(QNode));
if(!
Q.front)return;
Q.front->next=NULL;
return;
}
voidEnQueue(LinkQueue&Q,QElemTypee)
{
Queueptrp=NULL;
p=(Queueptr)malloc(sizeof(QNode));
if(!
p)return;
p->data=e;
p->next=NULL;
Q.rear->next=p;
Q.rear=p;
return;
}
QElemTypeDeQueue(LinkQueue&Q,QElemType&e)
{
Queueptrp;
if(Q.front==Q.rear)return'';
p=Q.front->next;
e=p->data;
Q.front->next=p->next;
if(Q.rear==p)
Q.rear=Q.front;
free(p);
returne;
}
intQueueEmpty(LinkQueueQ)
{
if(Q.front==Q.rear)
return1;
else
return0;
}
intLocate(ALGraphG,VertexTypev)
{
for(intk=0;k{
if(v==G.vertices[k].data)
returnk;
}
if(k=G.vexnum)
printf("输入的顶点不合法\n");
return0;
}
voidCreateALGraph(ALGraph&G)
{
VertexTypev1,v2;
inti,j,k;
ArcNode*p,*r;
printf("请输入顶点数和弧数(以空格分开):
");
scanf("%d%d",&G.vexnum,&G.arcnum);
for(i=0;i{
getchar();
printf("请输入第%d个结点:
",i);
scanf("%c",&G.vertices[i].data);
G.vertices[i].firstarc=NULL;
}
for(i=0;i{
printf("请输入第%d条弧(格式:
顶点顶点(以空格隔开)):
",i);
getchar();
scanf("%c%c",&v1,&v2);
k=Locate(G,v1);
j=Locate(G,v2);
p=(ArcNode*)malloc(sizeof(ArcNode));
r=(ArcNode*)malloc(sizeof(ArcNode));
p->adjvex=j;
p->info=NULL;
r->adjvex=k;
r->info=NULL;
p->nextarc=G.vertices[k].firstarc;
G.vertices[k].firstarc=p;
r->nextarc=G.vertices[j].firstarc;
G.vertices[j].firstarc=r;
}
return;
}
voidBFSTraverse(ALGraphG,QElemTypex)
{
inti,v;
ArcNode*p;
QElemTypev1;
for(v=0;vvisited[v]=false;
LinkQueueQ;
InitQueue(Q);
EnQueue(Q,x);
i=Locate(G,x);
visited[i]=true;
for(v=0;v{
while(!
QueueEmpty(Q))
{
DeQueue(Q,v1);
printf("%c",v1);
i=Locate(G,v1);
p=G.vertices[i].firstarc;
while(p!
=NULL)
{
if(!
visited[p->adjvex])
{
visited[p->adjvex]=true;
EnQueue(Q,G.vertices[p->adjvex].data);
}
p=p->nextarc;
}
}
if(!
visited[v])
{
visited[v]=true;
EnQueue(Q,G.vertices[v].data);
}
}
}
voidmain()
{
charflag1;
ALGraphgraph2;
QElemTypex;
CreateALGraph(graph2);
flag1='Y';
while(flag1=='Y'||flag1=='y')
{
printf("请输入遍历的起点:
");
getchar();
scanf("%c",&x);
printf("广度遍历结果是:
\n");
BFSTraverse(graph2,x);
printf("\n继续遍历(Y/N):
");
getchar();
scanf("%c",&flag1);
}
return;
}
3.
#include"stdio.h"
#include"stdlib.h"
#defineStackInitSize20
#defineStackIncrement5
#defineMaxVertexNum20
typedefintInfoType;
typedefcharVertexType;
typedefVertexTypeSElemType;
typedefstruct
{
SElemType*base;
SElemType*top;
intstacksize;
}SqStack;
typedefstructArcNode
{
intadjvex;//该弧指向的顶点位置
structArcNode*nextarc;//指向下一条弧的指针
InfoType*info;
}ArcNode;
typedefstructVNode
{
intindegree;
VertexTypedata;//顶点信息
ArcNode*firstarc;//指向第一条依附该顶点的弧的指针
}VNode,AdjList[MaxVertexNum];
typedefstruct
{
AdjListvertices;
intvexnum,arcnum;//图的当前顶点数和弧数
}ALGraph;
boolInitStack(SqStack&s)
{
s.base=(SElemType*)malloc(StackInitSize*sizeof(SElemType));
if(!
s.base)returnfalse;
s.top=s.base;
s.stacksize=StackInitSize;
returntrue;
}
boolPop(SqStack&s,int&e)
{
if(s.top==s.base)
returnfalse;
e=*--s.top;
returntrue;
}
boolPush(SqStack&s,inte)
{
if(s.top-s.base>=s.stacksize)
{
s.base=(SElemType*)realloc(s.base,(s.stacksize+StackIncrement)*sizeof(SElemType));
if(!
s.base)
returnfalse;
s.top=s.base+s.stacksize;
s.stacksize+=StackIncrement;
}
*s.top++=e;
returntrue;
}
boolStackEmpty(SqStacks)
{
if(s.top==s.base)
returntrue;
else
returnfalse;
}
intLocate(ALGraphG,VertexTypev)
{
for(intk=0;k{
if(v==G.vertices[k].data)
returnk;
}
if(k=G.vexnum)
printf("输入的顶点不合法\n");
return0;
}
voidCreateALGraph(ALGraph&G)//邻接表存储
{
VertexTypev1,v2;
inti,j,k;
ArcNode*p;
printf("请输入顶点数和弧数(以空格分开):
");
scanf("%d%d",&G.vexnum,&G.arcnum);
for(i=0;i{
getchar();
printf("请输入第%d个结点:
",i);
scanf("%c",&G.vertices[i].data);
G.vertices[i].firstarc=NULL;
G.vertices[i].indegree=0;
}
for(i=0;i{
printf("请输入第%d条有向弧弧(格式:
顶点顶点(以空格隔开)):
",i);
getchar();
scanf("%c%c",&v1,&v2);
k=Locate(G,v1);
j=Locate(G,v2);
p=(ArcNode*)malloc(sizeof(ArcNode));
p->adjvex=j;
p->info=NULL;
p->nextarc=G.vertices[k].firstarc;
G.vertices[k].firstarc=p;
}
return;
}
voidFindInDegree(ALGraphG,inta[MaxVertexNum])
{
inti,k;
ArcNode*p;
for(i=0;i{
for(p=G.vertices[i].firstarc;p;p=p->nextarc)
{
k=p->adjvex;
a[k]=++G.vertices[k].indegree;
}
}
return;
}
voidTopologicalSort(ALGraphG)//拓扑排序算法
{
inti,j,count;
ArcNode*p;
SqStacks;
intindegree[MaxVertexNum];
for(i=0;iindegree[i]=0;
FindInDegree(G,indegree);
InitStack(s);
for(i=0;i{
if(!
indegree[i])
Push(s,i);
}
count=0;
while(!
StackEmpty(s))
{
Pop(s,i);
printf("%c",G.vertices[i].data);
++count;
for(p=G.vertices[i].firstarc;p;p=p->nextarc)
{
j=p->adjvex;
if(!
(--indegree[j]))Push(s,j);
}
}
if(countprintf("错误!
该有向图有回路!
\n");
elsereturn;
}
voidmain()
{
ALGraphgraph3;
CreateALGraph(graph3);
printf("拓扑排序的结果是:
\n");
TopologicalSort(graph3);
printf("\n");
return;
}