数据结构图的深度广度优先遍历
#include <stdio.h>#include <alloc.h>
#define vextype int
#define MAX 100
int visitde;
typedef struct node
{
int adjvex;
struct node *next;
}edgenode;
typedef struct
{
vextype data;
edgenode *link;
}vexnode;
typedef struct
{
vexnode adjlist[MAX];
int n,e;
}adjlist;
adjlist creat_adjgraph(adjgraph *g)
{
edgenode *p;
int i,s,d;
printf("please input dingdianshu:");
scanf("%d",&s);
g->n=s;
printf("please input bianshu:");
scanf("%d",&d);
g->e=d;
for(i=1;1<=g->n;i++)
{
printf("di %d ge ding dian xin xi:",i);
scanf("%d",&g->adjlist[i-1].data);
g->adjlist[i-1].link=NULL;
}
for(i=1;i<=g->e;i++)
{
printf("di %d tiao bian qi shi ding dian bian hao he zhong zhi ding dian bian hao :",i);
scanf("%d,%d",&s,&d);
while(s<1||s>g->n||d<1||d>g->e)
{
prntf("bian hao chao chu fan wei,qing cong xin shu ru ;");
scanf("%d%d",&s,&d);
}
s--;
d--;
p=(edgenode*)malloc(sizeof(edgenode));
p->adjvex=d;
p->next=g->adjlist[s].link;
g->adjlist[s].link=p;
}
return *g;
}
creat_wxdadjlist(adjlist *q)
{
edgenode *s;
int i,j,k ,x;
printf("qing shu ru ding dian shu:");
scanf("%d",&j);
printf("qing shu ru bian shu :");
scanf("%d",&k);
for(i=1;i<=q->n;i++)
{
printf("\n qing shu ru di %d ge ding dian d xin xi:",i);
scanf("%d",q->adjlist[i-1].data);
q->adjlist[i-1].link=NULL;
}
for(i=1;i<=q->e;i++)
{
printf("\n qing shu ru tu zhong de bian xin xi;");
scanf("%d%d",&j,&k);
s=malloc(sizeof(edgenode));
s->adjvex=k;
s->next=adjlist[i].link;
adjlist.link=s;
s=malloc(sizeof(edgenode));
s->adjvex=j;
s->next=adjlist[k].link;
adjlist[k].link=s;
}
}
void dispagraph(adjlist *g)
{
int i;
edgenode *p;
printf("\n tu de lin jie biao:");
for(i=0;i<g->n;i++)
{
printf("%2d(%c)",i,g->adjlist[i].data); //here
p=g->adjlist[i].limk;
while (p!=NULL)
{
printf("-->(%d)",p->adjvex);
p=p->next;
}
printf("\n");
}
}
void dfs(_algraph*g,int vi)
{
int i;
arcnode *p;
int visitde[MAXEX],top=-1,stack[MAX];
for(i=0;i<MAXEX;i++)
visitde[i]=0;
cout<<v<<" ";
top++;
stock[top]=v;
visitde[v]=1;
while(top>=0)
{
v=stack[top];
top--;
p=g[v]->link;
while(p!=NULL&&visitde[p->adjvex]
p=p->nextarc;
if(p==NULL)
top--;
else
{
v=p->adjvex;
cout<<v<<" ";
visitde[v]=1;
top++
stack[top]=v;
}
viod bfs(adjlist *g,int vi)
int i,v,visitde[MAX];
int qu[MAX],front=0,rear=0;
edgenode *p;
for(i=0;i<g->n;i++)
visitde[i]=0;
printf("(%d",vi);
printf("%c)",g->adjlist[vi].data);
visitde[vi]=1; //here
rear=(rear+1)%MAX;
qu[rear]=vi;
while (front!=rear)
{
front=(front+1)%MAX;
v=qu[front];
p=g->adjlist[v].link;
while (p!=NULL)
{
if(visitde[p=->adjvex]==0)
{
visitde[p->dajvex]=1;
printf("(%d",p->adjvex);
printf("%c)",g->adjlist(p->adjvex);
rear=(rear+1%MAX;
qu[rear]=p->adjvex;
}
p=p->next;
}
meun graph( )
printf("-------------1 jian li you xiang tu----------------");
printf("-------------2 jian li wu xiang tu----------------");
printf("------------3 shen du you xian bian li-------------");
printf("------------4 guang du you xian bian li------------");
printf("-------------------0 tui chu-----------------------");
main()
int i,f;
char ch1,ch2,a;
adjlist g;
while(ch1=="y"||ch1=="Y")
{
meungraph();
scanf("%c",&ch2);//here
getchar();
switch(ch2)
{
case'1';
printf("jian li you xiang tu");
scanf("%d",&f);
adjpraph creat_adjgraph(&g,f);
dispagraph(&g);
break;
case'2':
printf("jian li wu xiang tu ");
scanf("%d",&f);
creat_wxdadjlist(&g,f);
dispagraph(&g);
break;
case'3':
printf("qing shu ru kai shi jin xing shen du you xian bian li de ding dian:");
scanf("%d",&f);
printf("\n cong ding dian %d kai shi shen du you xian biao li",i);
for(i=0;i<g->n;i++)
visitde[i]=0;
dfs(&g,f);
break;
case'4':
printf("qing shu ru kai shi jin xing guang du you xian bian li de ding dian:")
scanf("%d",&i);
printf("\n cong ding dian %d kai shi guang du you xian bian li",i);
bfs(&g,i);
break;
case'0';
ch1='n';
break;
default;
printf("shu ru you wu qing zai 0-4 zhong jin xing xuan ze");
if(ch2!='0')
{
printf("\n an hui che jian ji xu, an ren yi jian fan hui zhu han shu");
a=getchar();
if(a!="\xa")
{
getchar();ch1='n';
}
}
最后实行之后 出现Declaration syntax error 不知道怎么办?