程序在Dev里有时可以正常运行 有时读不进去东西 在pta里面交的话是编译错误
#include <stdio.h>
#include <stdlib.h>
struct stud_node {
int num;
char name[20];
int score;
struct stud_node *next;
};
struct stud_node *createlist();
struct stud_node *deletelist( struct stud_node *head, int min_score );
int main()
{
int min_score;
struct stud_node *p, *head = NULL;
head = createlist();
scanf("%d", &min_score);
head = deletelist(head, min_score);
for ( p = head; p != NULL; p = p->next )
printf("%d %s %d\n", p->num, p->name, p->score);
return 0;
}
/* 你的代码将被嵌在这里 */
struct stud_node *createlist()
{
stud_node *p,*head=(struct stud_node *)malloc(sizeof(struct stud_node*));
p=head;
int a;
scanf("%d",&a);
while(a!=0)
{
p->num=a;
scanf("%s%d",p->name,&p->score);
p->next=NULL;
scanf("%d",&a);
if(a)
{
p->next=(struct stud_node *)malloc(sizeof(struct stud_node *));
p=p->next;
}
else
break;
}
return head;
}
struct stud_node *deletelist( struct stud_node *head, int min_score )
{
struct stud_node *b,*p=head,*head1=head;
int first=1;
while(p!=NULL)
{
if(p->score<min_score)
{
if(first)
{
head1=p->next;
}
else
{
b->next=p->next;
free(p);
p=b->next;
continue;
}
}
else
{
first=0;
}
b=p;
p=p->next;
}
return head1;
}
将代码做个增减法,先从最小的代码量开始执行,然后再增加,直到出现异常就知道那里出错了
我发现的问题:
while (a != 0)
{
p->num = a;
char c;
while ((c = getchar()) != '\n');
//scanf_s("%s", p->name);
gets_s(p->name);
scanf_s("%d", &p->num);
p->next = NULL;
scanf_s("%d", &a);
if (a)
{
p->next = (struct stud_node *)malloc(sizeof(struct stud_node *));
p = p->next;
} else
break;
}
输入缓冲区数据没有清空,导致p->num输入错误,建议改成如上代码!
第二,deletelist函数中,score一直没有被赋值过,你就使用他进行判断,这怎么能行呢?
struct stud_node *createlist() {
stud_node *p, *head = (struct stud_node *)malloc(sizeof(struct stud_node*));
p = head;
int a;
scanf_s("%d", &a);
while (a != 0)
{
p->num = a;
char c;
while ((c = getchar()) != '\n');
//scanf_s("%s", p->name);
gets_s(p->name);
scanf_s("%d", &p->score);
p->next = NULL;
scanf_s("%d", &a);
if (a)
{
p->next = (struct stud_node *)malloc(sizeof(struct stud_node *));
p = p->next;
} else
break;
}
return head;
}
您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632
非常感谢您使用有问必答服务,为了后续更快速的帮您解决问题,现诚邀您参与有问必答体验反馈。您的建议将会运用到我们的产品优化中,希望能得到您的支持与协助!
速戳参与调研>>>https://t.csdnimg.cn/Kf0y
也许对你有帮助:https://blog.csdn.net/it_xiangqiang/category_10768339.html