Description:
用结构体建立学生信息,学生信息包括学号、姓名、成绩,建立一个有 n 名学生的链
表, 并将链表输出。
Input:
一次输入学生信息包括学号、姓名。0 0 0结束程序。
Sample Input:
C1001 Li 70
M1002 He 89
E1003 Xie 83
M1004 Wu 92
E1005 Bao 80
Sample Output:
C1001 Li 70
M1002 He 89
E1003 Xie 83
M1004 Wu 92
E1005 Bao 80
1 #include <stdio.h> 2 #include <stdlib.h> 3 struct student 4 { 5 char num[20]; 6 char name[20]; 7 float score; 8 struct student *next; 9 }; 10 int n; 11 struct student *creat() 12 { 13 struct student *head,*p1,*p2; 14 n=0; 15 p1=p2=(struct student *)malloc(sizeof(struct student)); 16 scanf("%s %s %f",p1->num,p1->name,&p1->score); 17 head=NULL; 18 while(p1->num!=0&&p1->name!=0&&p1->score!=0) 19 { 20 n+=1; 21 if(n==1) 22 head=p1; 23 else 24 p2->next=p1; 25 p2=p1; 26 p1=(struct student *)malloc(sizeof(struct student)); 27 scanf("%s %s %f",p1->num,p1->name,&p1->score); 28 } 29 p2->next=NULL; 30 return head; 31 } 32 void print(struct student *head) 33 { 34 struct student *p; 35 p=head; 36 if(head!=NULL) 37 { 38 do 39 { 40 printf("%s %s %g\n",p->num,p->name,p->score); 41 p=p->next; 42 } while(p!=NULL); 43 } 44 } 45 int main() 46 { 47 struct student *head; 48 head=creat(); 49 print(head); 50 return 0; 51 }
时间: 2024-10-10 01:46:04