数据结构实验报告图的深度优先遍历算法.docx

上传人:b****5 文档编号:30697163 上传时间:2023-08-19 格式:DOCX 页数:9 大小:331.56KB
下载 相关 举报
数据结构实验报告图的深度优先遍历算法.docx_第1页
第1页 / 共9页
数据结构实验报告图的深度优先遍历算法.docx_第2页
第2页 / 共9页
数据结构实验报告图的深度优先遍历算法.docx_第3页
第3页 / 共9页
数据结构实验报告图的深度优先遍历算法.docx_第4页
第4页 / 共9页
数据结构实验报告图的深度优先遍历算法.docx_第5页
第5页 / 共9页
点击查看更多>>
下载资源
资源描述

数据结构实验报告图的深度优先遍历算法.docx

《数据结构实验报告图的深度优先遍历算法.docx》由会员分享,可在线阅读,更多相关《数据结构实验报告图的深度优先遍历算法.docx(9页珍藏版)》请在冰豆网上搜索。

数据结构实验报告图的深度优先遍历算法.docx

数据结构实验报告图的深度优先遍历算法

 

题目:

图的深度优先遍历算法

一、实验题目

前序遍历二叉树

二、实验目的

⑴掌握图的逻辑结构;

⑵掌握图的邻接矩阵存储结构;

⑶验证图的邻接矩阵存储及其深度优先遍历操作的实现。

三、实验内容与实现

⑴建立无向图的邻接矩阵存储;

⑵对建立的无向图,进行深度优先遍历;实验实现

#include

#include

#defineMaxVex255

#defineTRUE1

#defineFALSE0

typedefcharVertexType;

typedefintBool;

Boolvisited[MaxVex];

typedefstructEdgeNode{

intadjvex;

structEdgeNode*next;

}EdgeNode;

typedefstructVertexNode{

VertexTypedata;

EdgeNode*firstedge;

}VertexNode,AdjList[MaxVex];

typedefstructGraph{

AdjListadjList;

intnumVertexes,numEdges;

}Graph,*GraphAdjList;

typedefstructLoopQueue{

intdata[MaxVex];

intfront,rear;

}LoopQueue,*Queue;

voidinitQueue(Queue&Q){

Q->front=Q->rear=0;

}

BoolQueueEmpty(Queue&Q){

if(Q->front==Q->rear){

returnTRUE;

}else{

returnFALSE;

}

}

BoolQueueFull(Queue&Q){

if((Q->rear+1)%MaxVex==Q->front){

returnTRUE;

}else{

returnFALSE;

}

}

 

voidEnQueue(Queue&Q,inte){

if(!

QueueFull(Q)){

Q->data[Q->rear]=e;

Q->rear=(Q->rear+1)%MaxVex;

}

}

 

voidDeQueue(Queue&Q,int*e){

if(!

QueueEmpty(Q)){

*e=Q->data[Q->front];

Q->front=(Q->front+1)%MaxVex;

}

}

 

voidCreateALGraph(GraphAdjList&G){/*建立图的邻接表结构*/

inti,j,k;

if(G==NULL){

G=(GraphAdjList)malloc(sizeof(Graph));

}

printf("输入图的结点数以及边数:

");

scanf("%d%d",&G->numVertexes,&G->numEdges);

fflush(stdin);

printf("===========================\n");

printf("输入各个顶点的数据:

\n");

for(i=0;inumVertexes;++i){

printf("顶点%d:

",i);

scanf("%c",&(G->adjList[i].data));

G->adjList[i].firstedge=NULL;

fflush(stdin);

}

printf("===========================\n");

for(k=0;knumEdges;++k){

printf("输入(vi,vj)上的顶点序号:

");

scanf("%d%d",&i,&j);

EdgeNode*ptrEdgeNode=(EdgeNode*)malloc(sizeof(EdgeNode));

ptrEdgeNode->adjvex=j;

ptrEdgeNode->next=G->adjList[i].firstedge;

G->adjList[i].firstedge=ptrEdgeNode;

ptrEdgeNode=(EdgeNode*)malloc(sizeof(EdgeNode));

ptrEdgeNode->adjvex=i;

ptrEdgeNode->next=G->adjList[j].firstedge;

G->adjList[j].firstedge=ptrEdgeNode;

}

}

voidDFS(GraphAdjList&G,inti){

visited[i]=TRUE;

printf("%c",G->adjList[i].data);

EdgeNode*p=G->adjList[i].firstedge;

while(p){

if(!

visited[p->adjvex]){

DFS(G,p->adjvex);//递归深度遍历

}

p=p->next;

}

}

 

/**

*深度优先遍历

*/

voidDFSTraverse(GraphAdjList&G){

inti;

for(i=0;inumVertexes;++i){

visited[i]=FALSE;//初始化访问数组visited的元素值为false

}

for(i=0;inumVertexes;++i){

if(!

visited[i]){//节点尚未访问

DFS(G,i);

}

}

}

 

/**

*图的广度优先遍历

*/

voidBFSTraverse(GraphAdjList&G){

inti;

QueueQ=(Queue)malloc(sizeof(LoopQueue));

for(i=0;inumVertexes;++i){

visited[i]=FALSE;

}

initQueue(Q);

for(i=0;inumVertexes;++i){

if(!

visited[i]){

visited[i]=TRUE;

printf("%c",G->adjList[i].data);

EnQueue(Q,i);

while(!

QueueEmpty(Q)){

DeQueue(Q,&i);

EdgeNode*p=G->adjList[i].firstedge;

while(p){

if(!

visited[p->adjvex]){

visited[p->adjvex]=TRUE;

printf("%c",G->adjList[p->adjvex].data);

EnQueue(Q,p->adjvex);

}

p=p->next;

}

}

}

}

}

intmain(){

GraphAdjListG=NULL;

CreateALGraph(G);

printf("\n图的深度优先遍历为:

");

DFSTraverse(G);

printf("\n图的广度优先遍历为:

");

BFSTraverse(G);

printf("\n");

return0;

}

 

四、实验心得

⑴建立了无向图的邻接矩阵存储;

⑵掌握了对所建立的无向图的深度优先遍历和广度优先遍历;

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > IT计算机 > 计算机硬件及网络

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1