写一个程序,定义以下结构类型,调用函数创建一个动态链表,并且输出该链表。 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-02-05 20:31:12 浏览: 34
以下是一个使用 C 语言实现动态链表的程序,它满足你给出的测试用例的要求。
```
#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_student(int num, char *name, float *score) {
struct Student *student = (struct Student*)malloc(sizeof(struct Student));
student->num = num;
strcpy(student->name, name);
student->score[0] = score[0];
student->score[1] = score[1];
student->score[2] = score[2];
student->next = NULL;
return student;
}
// 将一个学生结点插入到动态链表中
void insert_student(struct Student **head, struct Student *student) {
if (*head == NULL) {
*head = student;
} else {
struct Student *current = *head;
while (current->next != NULL) {
current = current->next;
}
current->next = student;
}
}
// 打印动态链表中的所有学生信息
void print_students(struct Student *head) {
struct Student *current = head;
while (current != NULL) {
printf("%d, %s, %.2f, %.2f, %.2f\n",
current->num, current->name, current->score[0], current->score[1], current->score[2]);
current = current->next;
}
}
int main() {
// 创建一个动态链表头指针
struct Student *head = NULL;
// 循环获取用户输入的学生信息并插入到动态链表中
while (1) {
printf("请输入学生信息(学号 姓名 成绩1 成绩2 成绩3):");
int num;
char name[20];
float score[3];
scanf("%d %s %f %f %f", &num, name, &score[0], &score[1], &score[2]);
// 如果