小小的链表问题

QQWDYY 2009-10-27 08:04:25
#include"stdio.h"
#include"malloc.h"
#include"stdlib.h"
struct Student
{
char ID[20];
char name[50];
float score;
struct Student *next;
};
main()
{
struct Student *linkhead,*linktail,*student;
float avescore;
int totalstudent;
linkhead=linktail=NULL;
while(1)
{

student=malloc(sizeof(struct Student));
scanf("%s %s %f",student->ID,student->name,&student->score);
if(student->score<0)
{
free(student);
break;
}
student->next=NULL;
if(linktail==NULL)
{
linkhead=linktail=student;
}
else
{
linktail->next=student;
linktail=student;
}
}
avescore=0.0;
totalstudent=0;
while(student!=NULL)
{
totalstudent++;
avescore=avescore+student->score;
student=student->next;
}
avescore=avescore/totalstudent;
printf("平均分:%5.2f\n",avescore);
while(linkhead!=NULL)
{
student=linkhead;
linkhead=student->next;
free(student);
}
return 0;
}

可以输入数据 但是一算结果就错误了
...全文
55 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
kouwenlong 2009-10-27
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 aaajj 的回复:]
这样应该就可以了
C/C++ code#include"stdio.h"
#include"malloc.h"
#include"stdlib.h"struct Student
{char ID[20];char name[50];float score;struct Student*next;
};
main()
{struct Student*linkhead,*linktail,*student;float avescore;int totalstudent;
linkhead=linktail=NULL;while(1)
{

student=malloc(sizeof(struct Student));
scanf("%s %s %f",student->ID,student->name,&student->score);
student->next=NULL;//////////////////if(student->score<0)
{
free(student);break;
}if(linktail==NULL)
{
linkhead=linktail=student;
}else
{
linktail->next=student;
linktail=student;
}
}
avescore=0.0;
totalstudent=0;
student= linkhead;//////////while(student!=NULL)
{
totalstudent++;
avescore=avescore+student->score;
student=student->next;
}
avescore=avescore/totalstudent;
printf("平均分:%5.2f\n",avescore);while(linkhead!=NULL)
{
student=linkhead;
linkhead=student->next;
free(student);
}return0;
}
[/Quote]
楼上的还少了一点。如果不改这点,估计会报错吧。
student=malloc(sizeof(struct Student));应该改为student=(struct student *)malloc(sizeof(struct Student));
aaajj 2009-10-27
  • 打赏
  • 举报
回复

这样应该就可以了
#include"stdio.h"
#include"malloc.h"
#include"stdlib.h"
struct Student
{
char ID[20];
char name[50];
float score;
struct Student *next;
};
main()
{
struct Student *linkhead,*linktail,*student;
float avescore;
int totalstudent;
linkhead=linktail=NULL;
while(1)
{

student=malloc(sizeof(struct Student));
scanf("%s %s %f",student->ID,student->name,&student->score);
student->next=NULL;//////////////////

if(student->score<0)
{
free(student);
break;
}
if(linktail==NULL)
{
linkhead=linktail=student;
}
else
{
linktail->next=student;
linktail=student;
}
}
avescore=0.0;
totalstudent=0;
student = linkhead;//////////
while(student!=NULL)
{
totalstudent++;
avescore=avescore+student->score;
student=student->next;
}
avescore=avescore/totalstudent;
printf("平均分:%5.2f\n",avescore);
while(linkhead!=NULL)
{
student=linkhead;
linkhead=student->next;
free(student);
}
return 0;
}

70,037

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧