输入若干个学生的信息(学号、姓名、成绩),当输入学号为0时结束,用单向链表组织这些学生信息后,再按顺序输出。 输入样例: 1 zhang 78 2 wang 80 3 li 75 4 zhao 85 0 输出样例: 1 zhang 78 2 wang 80 3 li 75 4 zhao 85 用c语言程序写代码
时间: 2024-03-17 21:47:17 浏览: 56
1_lower7y7_定义一个包含学生信息(学号_成绩)的顺序表和链表(二选一)_姓名_使其具有如下功能_
5星 · 资源好评率100%
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct student {
int num;
char name[20];
int score;
struct student* next;
};
int main() {
struct student* head = NULL;
struct student* p, *tail;
int num, score;
char name[20];
while (1) {
scanf("%d", &num);
if (num == 0) {
break;
}
scanf("%s%d", name, &score);
p = (struct student*)malloc(sizeof(struct student));
p->num = num;
strcpy(p->name, name);
p->score = score;
p->next = NULL;
if (head == NULL) {
head = p;
tail = p;
} else {
tail->next = p;
tail = p;
}
}
printf("学号\t姓名\t成绩\n");
p = head;
while (p != NULL) {
printf("%d\t%s\t%d\n", p->num, p->name, p->score);
p = p->next;
}
p = head;
while (p != NULL) {
tail = p->next;
free(p);
p = tail;
}
return 0;
}
```
阅读全文