#include <stdio.h>
#include <stdlib.h>
typedef struct student
{
int ID;
char name[20];
float score;
struct student *next;
}Stu;
Stu *jianli()
{
Stu *p1,*p2,*head=NULL;
do
{
p1=(Stu *)malloc(sizeof(Stu));
if(head==NULL) head=p1;
else p2->next=p1;
p2=p1;
p2->next=NULL;
scanf("%d,%s,%f",&p2->ID,p2->name,&p2->score);
}while(p2->ID!=0);
return head;
}
void show(Stu *head)
{
Stu *p=head;
while(p->next!=NULL)
{
printf("%d,%s,%f\n",p->ID,p->name,p->score);
p=p->next;
}
}
int main()
{
Stu *s;
s=jianli();
show(s);
return 0;
}
2 回答
慕粉13202587281
TA贡献9条经验 获得超1个赞
建立链表的时候错了.
head->next = NULL; p2 = head; while(1){ if((p1 = (Stu*)malloc(sizeof(Stu))) == NULL){exit(0);} scanf("%d,%s,%f",&p1->ID,p1->name,&p1->score); if(p1->ID == 0){ free(p1); break; }else{ p2->next = p1; p1->next = NULL; p2 = p1; } }
- 2 回答
- 0 关注
- 1362 浏览
添加回答
举报
0/150
提交
取消