输入若干个学生的信息(学号、姓名、成绩),当输入学号为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-21 08:40:18 浏览: 95
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Student {
int num;
char name[20];
int score;
struct Student *next;
};
struct Student *createList() {
struct Student *head, *tail, *node;
int num, score;
char name[20];
head = tail = NULL;
scanf("%d", &num);
while (num != 0) {
scanf("%s%d", name, &score);
node = (struct Student *)malloc(sizeof(struct Student));
node->num = num;
strcpy(node->name, name);
node->score = score;
node->next = NULL;
if (head == NULL) {
head = tail = node;
} else {
tail->next = node;
tail = node;
}
scanf("%d", &num);
}
return head;
}
void printList(struct Student *head) {
struct Student *p = head;
while (p != NULL) {
printf("%d %s %d\n", p->num, p->name, p->score);
p = p->next;
}
}
int main() {
struct Student *head;
head = createList();
printList(head);
return 0;
}
```
阅读全文
相关推荐
















