#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條經(jīng)驗 獲得超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 關(guān)注
- 1422 瀏覽
添加回答
舉報
0/150
提交
取消