求教:C语言问题

2025-02-03 00:03:19
推荐回答(2个)
回答1:

很多的是最基本的错误:
1.少分号
2.逗号写成了点
3.点写成了逗号
4.int locatevex(struct vex*w,int vvnum)应该是int locatevex(struct vex*w,char v,int vvnum)
不具体举出来了
帮你把错误改了,还没看你程序想要实现什么
#include
#include
#include
struct node{
int adjvex;
struct node* next;
};
struct vex{
char data;
struct node* firstarc;
};
int locatevex(struct vex*w,char v,int vvnum)
{
int i;
for(i=0;i {
if(w[i].data==v)
{
return i;
}
}
return -1;
}
struct vex* create(struct vex*s,int vnum,int anum)
{
int i,j,k;
struct node*p;char ss[3];
for(i=0;i {
s[i].data=getchar();
s[i].firstarc=NULL;
}
for(k=0;k {
gets(ss);
i=locatevex(s,ss[0],vnum);
j=locatevex(s,ss[1],vnum);
if(i<0||j<0)
{
printf("error arc");
k++;
continue;
}
else
{
p=(struct node*)malloc(sizeof(struct node));
p->adjvex=j;
p->next=s[i].firstarc;
s[i].firstarc=p;
}
}//end of k
return s;
}
void bfs(struct vex*s,int vnum,int v )
{
int visited[20],q[20],front=0,rear=0,w;
struct node *p;
for(w=0;w visited[w]=0;
printf("%c",s[v].data);
q[rear++]=v;
while(front!=rear)
{
w=q[front++];
p=s[w].firstarc;
while(front!=rear)
{
v=p->adjvex;
if(!visited[v])
{
printf("%c",s[v].data);
visited[v]=1;
q[rear++]=v;
}//end of if
}//end of while of p;
}
}
int main()
{
struct vex a[20],*p;
int vexnum,arcnum;
scanf("vex=%d,arc=%d",&vexnum,&arcnum);
p=create(a,vexnum,arcnum);
bfs(a,vexnum,2);
return 0;
}

回答2:

1、locatevex的声明少写了第2个参数
2、create的声明中,参数的类型structvex连写了。紧接着函数体中第一句k前面的逗号写成了点
3、create函数体中,printf("error arc")忘加分号了
4、create函数体中,p=(struct node *)malloc(sizeof(struct node));注意强制转换是struct node *
5、bfs函数体第3行,num改成vnum
6、bfs函数体第9行,p=s[w],firstarc;改成p=s[w].firstarc;
7、bfs函数体倒数第3行,rear写成了redr
8、bfs函数体最后少写了个}
9、main函数中调用create时,把create写成了creat,把vexnum写成了nexnum。而且上一句的scanf末尾没写分号

#include
#include
#include
struct node{
int adjvex;
struct node* next;
};
struct vex{
char data;
struct node* firstarc;
};
int locatevex(struct vex*w,char v,int vvnum)
{int i;
for(i=0;i if(w[i].data==v) return i;
return -1;
}
struct vex* create(struct vex*s,int vnum,int anum)
{int i,j,k;
struct node*p;char ss[3];
for(i=0;i {s[i].data=getchar();
s[i].firstarc=NULL;
}
for(k=0;k{gets(ss);
i=locatevex(s,ss[0],vnum);
j=locatevex(s,ss[1],vnum);
if(i<0||j<0)
{printf("error arc");
k++;
continue;
}
else
{p=(struct node *)malloc(sizeof(struct node));
p->adjvex=j;
p->next=s[i].firstarc;
s[i].firstarc=p;
}
}//end of k
return s;
}
void bfs(struct vex*s,int vnum,int v )
{int visited[20],q[20],front=0,rear=0,w;
struct node *p;
for(w=0;w visited[w]=0;
printf("%c",s[v].data);
q[rear++]=v;
while(front!=rear)
{w=q[front++];
p=s[w].firstarc;
while(front!=rear)
{v=p->adjvex;
if(!visited[v])
{printf("%c",s[v].data);
visited[v]=1;
q[rear++]=v;
}//end of if
}//end of while of p;
}
}
main()
{struct vex a[20],*p;
int vexnum,arcnum;
scanf("vex=%d,arc=%d",&vexnum,&arcnum);
p=create(a,vexnum,arcnum);
bfs(a,vexnum,2);
}