#include "stdio.h"#include<malloc.h>#include<string.h>#include<ctype.h>#define LEN struct studentstruct student{ int num; int score; struct student*next;};struct student*create(){ char ch;? ? struct student *head;? ? struct student*p1,*p2;? ? int a=0;? ? printf("是否輸入新數(shù)據(jù):\n");? ? while(toupper(ch=getchar())=='Y')? ? {? ? ? p1=(LEN*)malloc(sizeof(LEN));? ? ? scanf("%d%d",&(*p1).num,&(*p1).score);? ? ? a++;? ? ? if(a==1)? ? ? ?head=p1;? ? ? else? ? ? ?(*p2).next=p1;? ? ? p2=p1;? ? ? p1->next=NULL;? ? ? }? ? return(head);}void print(struct student *head){ struct student *p; p=head; while(p!=NULL) { printf("\n學(xué)號(hào):%d,成績(jī):%d",p->num,p->score); p=p->next; }}void main( ){ print(create());}
1 回答
已采納

zy112
TA貢獻(xiàn)8條經(jīng)驗(yàn) 獲得超10個(gè)贊
getchar() 會(huì)接受回車(chē), 所以你在輸入了學(xué)號(hào)和成績(jī)之后,你會(huì)輸入回車(chē),然后getchar() 就會(huì)接受回車(chē), 不等于 Y , 所以輸入就結(jié)束了, 你在while 后面輸入一個(gè)getchar()就可以了。。如下。
struct?student*create() { char?ch; ????struct?student?*head; ????struct?student*p1,*p2; ????int?a=0; ????printf("是否輸入新數(shù)據(jù):\n"); ????while(toupper(ch=getchar())=='Y') ????{ ??????p1=(LEN*)malloc(sizeof(LEN)); ??????scanf("%d%d",&(*p1).num,&(*p1).score); ??????a++; ??????if(a==1) ???????head=p1; ??????else ???????(*p2).next=p1; ??????p2=p1; ??????p1->next=NULL; ?????? ??????getchar(); ??????} ????return(head); }
- 1 回答
- 0 關(guān)注
- 1470 瀏覽
添加回答
舉報(bào)
0/150
提交
取消