数据结构图的深度遍历DFS和广度遍历BFSC语言
#include
#include
#define NULL 0
#define W (struct arcnode*)malloc(sizeof(struct arcnode))
struct arcnode
{
int adjvex;
int info;
struct arcnode *next;
};
struct vnode
{
char vertex;
struct arcnode *firstedge; };
struct graph
{
struct vnode vertices[100];
int vexnum;
int arcnum;
};
struct qnode
{
int data;
struct qnode *next;
};
struct que
{
struct qnode *front;
struct qnode *rear;
};
void main()
{
struct graph *Create(void);
void DFS(struct graph *G,int *V);
void DD(struct arcnode*p,struct graph *G,int *V);
void BFS(struct graph *G,int *V);
void BB(struct que *Q,struct graph *G,int *V);
void FF(struct que *Q,struct arcnode *p,struct graph *G,int *V);
void inque(struct que *Q,int a);
void outque(struct que* Q);
int n,A[100]={0},*V;
struct graph * G;
V=&A[0];
G=Create();
printf("\nDFS=");
DFS(G,V);
printf("\nBFS=");
BFS(G,V);
}
struct graph *Create(void) {
int a,f,i;
char s[101];
struct arcnode *p,*q;
struct graph *H;
H=(struct graph*)malloc(sizeof(struct graph));
printf("\nInput vexnum and arcnum:");
scanf("%d %d",&a,&f);
H->vexnum=a;
H->arcnum=f;
printf("\nInput all the vertexes:");
scanf("%s",s);
for(i=0;i<(H->vexnum);i++)
{H->vertices[i].vertex=s[i];
H->vertices[i].firstedge=NULL;
}
printf("\nInput the adjvex table:");
for(i=0;ivexnum;i++)
{
scanf("%d",&a);
if(a>=0&&avexnum)
{
p=W;
p->next=NULL;
H->vertices[i].firstedge=p;
scanf("%d",&(p->info));
p->adjvex=a;
for(scanf("%d",&a);a>=0&&avexnum;)
{
q=W;
q->next=NULL;
p->next=q;
q->adjvex=a;
scanf("%d",&(q->info));
p=q;
scanf("%d",&a);
}
}
}
return H;
}
void DFS(struct graph *G,int *V) {
int i;
struct arcnode *p;
V[0]=1;
printf("%c",G->vertices[0]);
for(i=0;ivexnum;i++)
DD(G->vertices[i].firstedge,G,V); for(i=0;ivexnum;i++)
V[i]=0;
return;
}
void DD(struct arcnode *p,struct graph *G,int *V)
{
struct arcnode *q;
if(p!=NULL)
{
if(V[p->adjvex]==0)
{
printf("%c",G->vertices[p->adjvex].vertex);
V[p->adjvex]=1;
q=G->vertices[p->adjvex].firstedge;
DD(q,G,V);
}
DD(p->next,G,V);
}
return ;
}
void BFS(struct graph *G,int *V)
{
int i;
struct que *Q;
struct qnode *p;
p=(struct qnode*)malloc(sizeof(struct qnode));
p->next=NULL;
Q=(struct que*)malloc(sizeof(struct que));
Q->front=p;
Q->rear=Q->front;
for(i=0;ivexnum;i++)
{
if(V[i]==0)
{
V[i]=1;
printf("%c",G->vertices[i].vertex);
inque(Q,i);
BB(Q,G,V);
}
}
return;
}
void BB(struct que *Q,struct graph *G,int *V)
{
for(;Q->front!=Q->rear;outque(Q))
FF(Q,G->vertices[Q->front->next->data].firstedge,G,V);
}
void FF(struct que *Q,struct arcnode *p,struct graph *G,int *V)
{
if(p!=NULL)
{
if(V[p->adjvex]==0)
{
printf("%c",G->vertices[p->adjvex].vertex);
V[p->adjvex]=1;
inque(Q,p->adjvex);
}
FF(Q,p->next,G,V);
}
return;
}
void inque(struct que *Q,int a)
{
struct qnode *p;
p=(struct qnode*)malloc(sizeof(struct qnode));
if(p!=NULL)
{
p->data=a;
p->next=NULL;
Q->rear->next=p;
Q->rear=p;
}
}
void outque(struct que *Q)
{
struct qnode *p;
if(Q->front!=Q->rear)
p=(Q->front)->next;
Q->front->next=p->next;
free(p);
}
本文档为【数据结构图的深度遍历DFS和广度遍历BFSC语言】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑,
图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。