写一个程序,定义以下结构类型,调用函数创建一个动态链表,并且输出该链表。 struct Student { int num; char name[20]; float score[3]; struct Student * next; }; 并且通过调用函数实现链表的创建,插入,打印等功能 测试用例:程序执行, 提示请用户输入学生数据, 用户输入: 1 zhang 70 80 90回车, 程序执行,提示请用户输入结点数据, 用户输入: 2 li 70 80 90回车, 程序执行,提示请用户输入结点数据, 用户输入: 3 wang 70 80 90回车, 程序执行,提示请用户输入结点数据, 用户输入: 0 0 0 0 0回车, 程序输出: 1, zhang, 70.00, 80.00, 90.00, 2, li, 70.00, 80.00, 90.00, 3, wang, 70.00, 80.00, 90.00
时间: 2023-01-28 19:31:59 浏览: 65
以下是一个示例程序,实现了链表的创建、插入和输出功能。请注意,这只是一个示例,并不是唯一的实现方式。
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Student {
int num;
char name[20];
float score[3];
struct Student *next;
};
struct Student *create_list() {
// 创建一个新的结点
struct Student *new_node = (struct Student*)malloc(sizeof(struct Student));
printf("请输入学生数据:");
scanf("%d%s%f%f%f", &new_node->num, new_node->name, &new_node->score[0], &new_node->score[1], &new_node->score[2]);
new_node->next = NULL; // 将 next 指针设置为 NULL
return new_node;
}
void insert_node(struct Student *head, struct Student *new_node) {
struct Student *p = head;
while (p->next != NULL) {
p = p->next; // 找到链表的最后一个结点
}
p->next = new_node; // 将新结点插入到最后
}
void print_list(struct Student *head) {
struct Student *p = head;
while (p != NULL) {
printf("%d, %s, %.2f, %.2f, %.2f\n", p->num, p->name, p->score[0], p->score[1], p->score[2]);
p = p->next; // 指向下一个结点
}
}
int main() {
struct Student *head = create_list(); // 创建链表的第一个结点
int num;
while (1) {
printf("请输入结点数据:");
scanf("%d", &num);
if (num == 0) { // 输入 0 结束循环
break;
}
struct Student *new_node = create_list();
insert_node(head, new_node);
}
print_list(head); // 输出链表
return 0;
}
```
执行测试用例时