实验11-2-1 建立学生信息链表(20 分)
本题要求实现一个将输入的学生成绩组织成单向链表的简单函数。
函数接口定义:
该函数利用scanf从输入中获取学生的信息,并将其组织成单向链表。链表节点结构定义如下:
| 12
 3
 4
 5
 6
 
 | struct stud_node {int              num;      /*学号*/
 char             name[20]; /*姓名*/
 int              score;    /*成绩*/
 struct stud_node *next;    /*指向下个结点的指针*/
 };
 
 | 
单向链表的头尾指针保存在全局变量head和tail中。
输入为若干个学生的信息(学号、姓名、成绩),当输入学号为0时结束。
裁判测试程序样例:
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 
 | #include <stdio.h>#include <stdlib.h>
 #include <string.h>
 
 struct stud_node {
 int    num;
 char   name[20];
 int    score;
 struct stud_node *next;
 };
 struct stud_node *head, *tail;
 
 void input();
 
 int main()
 {
 struct stud_node *p;
 
 head = tail = NULL;
 input();
 for ( p = head; p != NULL; p = p->next )
 printf("%d %s %d\n", p->num, p->name, p->score);
 
 return 0;
 }
 
 
 
 | 
输入样例:
| 12
 3
 4
 5
 
 | 1 zhang 782 wang 80
 3 li 75
 4 zhao 85
 0
 
 | 
输出样例:
| 12
 3
 4
 
 | 1 zhang 782 wang 80
 3 li 75
 4 zhao 85
 
 | 
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 
 | #include <stdio.h>#include <stdlib.h>
 #include <string.h>
 
 struct stud_node {
 int    num;
 char   name[20];
 int    score;
 struct stud_node *next;
 };
 struct stud_node *head, *tail;
 
 void input();
 
 int main()
 {
 struct stud_node *p;
 
 head = tail = NULL;
 input();
 for ( p = head; p != NULL; p = p->next )
 printf("%d %s %d\n", p->num, p->name, p->score);
 
 return 0;
 }
 
 void input()
 {
 struct stud_node *q;
 q=(struct stud_node *)malloc(sizeof(struct stud_node));
 scanf("%d", &q->num);
 while(q->num != 0)
 {
 scanf("%s %d", q->name, &q->score);
 if(head == NULL)
 {
 head = q;
 head->next = NULL;
 }
 if(tail != NULL)
 {
 tail->next = q;
 }
 tail = q;
 tail->next = NULL;
 q=(struct stud_node *)malloc(sizeof(struct stud_node));
 scanf("%d", &q->num);
 }
 }
 
 |