求解答,为什么深度 广度不能一起运行?怎么改?
#define MAX_VEX 100#include<stdio.h>
#include<malloc.h>
typedef struct node
{
int adjvex;
struct node * next;
}ARCNODE;
typedef struct vexnode
{
int vertex;
ARCNODE *firstarc;
}VEXNODE;
VEXNODE adjlist[MAX_VEX];
void bfs(int v);
void dfs(int v);
int creatadjlist()
{
ARCNODE * ptr;
int arcnum,vexnum,k,v1,v2;
printf("请输入顶点数和边数(输入格式为:顶点数,边数):");
scanf("%d,%d",&vexnum,&arcnum);
for(k=1;k<=vexnum;k++)
adjlist[k].firstarc=0;
for(k=0;k<arcnum;k++)
{
printf("v1,v2=");
scanf("%d,%d",&v1,&v2);
ptr=(ARCNODE *)malloc(sizeof(ARCNODE));
ptr->adjvex=v2;
ptr->next=adjlist[v1].firstarc;
adjlist[v1].firstarc=ptr;
ptr=(ARCNODE *)malloc(sizeof(ARCNODE));
ptr->adjvex=v1;
ptr->next=adjlist[v2].firstarc;
adjlist[v2].firstarc=ptr;
}
return(vexnum);
}
void dfs(int v)
{
int w;
ARCNODE *p;
p=adjlist[v].firstarc;
printf("%d",v);
adjlist[v].vertex=1;
while(p!=NULL)
{
w=p->adjvex;
if(adjlist[w].vertex==0)
dfs(w);
p=p->next;
}
}
void bfs(int v)
{
int queue[MAX_VEX];
int front=0,rear=1;
ARCNODE *p;
p=adjlist[v].firstarc;
printf("%d",v);
adjlist[v].vertex=1;
queue[rear]=v;
while(front!=rear)
{
front=(front+1)%MAX_VEX;
v=queue[front];
p=adjlist[v].firstarc;
while(p!=NULL)
{
if(adjlist[p->adjvex].vertex==0)
{
adjlist[p->adjvex].vertex=1;
printf("%d",p->adjvex);
rear=(rear+1)%MAX_VEX;
queue[rear]=p->adjvex;
}
p=p->next;
}
}
}
main()
{
int i,n,v;
ARCNODE *p;
n=creatadjlist();
printf("所见图的邻接表为:\n");
for(i=1;i<=n;i++)
{
printf("%d==>",i);
p=adjlist[i].firstarc;
while(p!=NULL)
{
printf("-- -->%d",p->adjvex);
p=p->next;
}
printf("\n");
}
printf("输入深度优先搜索起始顶点v:");
scanf("%d",&v);
printf("图的深度优先搜索序列DFS:");
dfs(v);
printf("\n");
printf("输入广度优先搜索起始顶点v:");
scanf("%d",&v);
printf("图的广度优先搜索序列BFS:");
bfs(v);
}