欢迎来到天天文库
浏览记录
ID:49471538
大小:72.50 KB
页数:12页
时间:2020-03-01
《数据结构C语言实现图的建立,图的广度,深度遍历.doc》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、.图的建立,图的广度,深度遍历#include"stdio.h"#definemaxsize1000#definen100typedefstruct{charvexs[n];intarcs[n][n];intnum;}G;typedefstruct{intdata[maxsize];intfront,rear;}V;voidGInit(G*L){L->num=0;}intGVexs(G*L)Word资料.{return(L->num);}voidGCreate(G*L){inti,j;GInit(L);printf("请输入
2、顶点数目:");scanf("%d",&L->num);printf("请输入各顶点:");for(i=0;inum;i++){fflush(stdin);scanf("%c",&L->vexs[i]);}printf("请输入各顶点边的关系(1是有关0是无关):");for(i=0;inum;i++){for(j=0;jnum;j++)Word资料.{scanf("%d",&L->arcs[i][j]);}}}voidGOut(GL){inti,j;printf("图的顶点数目为:%
3、d",L.num);printf("图的各顶点的信息为:");for(i=0;i4、for(v1=0;v15、=v%g.num;if(mark[v1]==0)DFS(g,v1,mark);}}voidQueueInit(V*sq){Word资料.sq->front=0;sq->rear=0;}intQueueIsEmpty(Vsq){if(sq.rear==sq.front)return(1);elsereturn(0);}intQueueFront(Vsq,int*e){if(QueueIsEmpty(sq)){printf("queueisempty!");return0;}else{*e=sq.data[(sq.front6、)];return1;}}intQueueIn(V*sq,intx)Word资料.//{if(sq->front==(sq->rear+1)%maxsize){printf("queueisfull!");return0;}else{sq->data[sq->rear]=x;sq->rear=(sq->rear+1)%maxsize;return(1);}}intQueueOut(V*sq){if(QueueIsEmpty(*sq)){printf("queueisempty!");return0;Word资料.}e7、lse{sq->front=(sq->front+1)%maxsize;return1;}}voidBFS(Gg,intv,intmark[]){intv1,v2;Vq;QueueInit(&q);QueueIn(&q,v);mark[v]=1;printf("%c",g.vexs[v]);while(QueueIsEmpty(q)==0){QueueFront(q,&v1);QueueOut(&q);for(v2=0;v28、=0){QueueIn(&q,v2);mark[v2]=1;printf("%c",g.vexs[v2]);}}}}voidGBFS(Gg){intqidian,v,v1,mark[maxsize];printf("广度遍历:");printf("");scanf("%d",&qi
4、for(v1=0;v15、=v%g.num;if(mark[v1]==0)DFS(g,v1,mark);}}voidQueueInit(V*sq){Word资料.sq->front=0;sq->rear=0;}intQueueIsEmpty(Vsq){if(sq.rear==sq.front)return(1);elsereturn(0);}intQueueFront(Vsq,int*e){if(QueueIsEmpty(sq)){printf("queueisempty!");return0;}else{*e=sq.data[(sq.front6、)];return1;}}intQueueIn(V*sq,intx)Word资料.//{if(sq->front==(sq->rear+1)%maxsize){printf("queueisfull!");return0;}else{sq->data[sq->rear]=x;sq->rear=(sq->rear+1)%maxsize;return(1);}}intQueueOut(V*sq){if(QueueIsEmpty(*sq)){printf("queueisempty!");return0;Word资料.}e7、lse{sq->front=(sq->front+1)%maxsize;return1;}}voidBFS(Gg,intv,intmark[]){intv1,v2;Vq;QueueInit(&q);QueueIn(&q,v);mark[v]=1;printf("%c",g.vexs[v]);while(QueueIsEmpty(q)==0){QueueFront(q,&v1);QueueOut(&q);for(v2=0;v28、=0){QueueIn(&q,v2);mark[v2]=1;printf("%c",g.vexs[v2]);}}}}voidGBFS(Gg){intqidian,v,v1,mark[maxsize];printf("广度遍历:");printf("");scanf("%d",&qi
5、=v%g.num;if(mark[v1]==0)DFS(g,v1,mark);}}voidQueueInit(V*sq){Word资料.sq->front=0;sq->rear=0;}intQueueIsEmpty(Vsq){if(sq.rear==sq.front)return(1);elsereturn(0);}intQueueFront(Vsq,int*e){if(QueueIsEmpty(sq)){printf("queueisempty!");return0;}else{*e=sq.data[(sq.front
6、)];return1;}}intQueueIn(V*sq,intx)Word资料.//{if(sq->front==(sq->rear+1)%maxsize){printf("queueisfull!");return0;}else{sq->data[sq->rear]=x;sq->rear=(sq->rear+1)%maxsize;return(1);}}intQueueOut(V*sq){if(QueueIsEmpty(*sq)){printf("queueisempty!");return0;Word资料.}e
7、lse{sq->front=(sq->front+1)%maxsize;return1;}}voidBFS(Gg,intv,intmark[]){intv1,v2;Vq;QueueInit(&q);QueueIn(&q,v);mark[v]=1;printf("%c",g.vexs[v]);while(QueueIsEmpty(q)==0){QueueFront(q,&v1);QueueOut(&q);for(v2=0;v28、=0){QueueIn(&q,v2);mark[v2]=1;printf("%c",g.vexs[v2]);}}}}voidGBFS(Gg){intqidian,v,v1,mark[maxsize];printf("广度遍历:");printf("");scanf("%d",&qi
8、=0){QueueIn(&q,v2);mark[v2]=1;printf("%c",g.vexs[v2]);}}}}voidGBFS(Gg){intqidian,v,v1,mark[maxsize];printf("广度遍历:");printf("");scanf("%d",&qi
此文档下载收益归作者所有