编写C语言程序,将20名学生的四门课程成绩表存放在一个链表中并且输出该链表各结点的值
时间: 2024-02-15 13:02:08 浏览: 30
好的,以下是可以参考的C语言程序,其中使用了链表来存储学生的成绩信息:
```c
#include <stdio.h>
#include <stdlib.h>
struct student {
char name[20];
int c_score;
int db_score;
int math_score;
int physics_score;
struct student *next;
};
int main() {
struct student *head = NULL, *p = NULL;
// 创建链表
for (int i = 0; i < 20; i++) {
struct student *node = (struct student *)malloc(sizeof(struct student));
printf("请输入第%d个学生的信息:\n", i + 1);
printf("姓名:");
scanf("%s", node->name);
printf("C语言成绩:");
scanf("%d", &node->c_score);
printf("数据库成绩:");
scanf("%d", &node->db_score);
printf("数学成绩:");
scanf("%d", &node->math_score);
printf("物理成绩:");
scanf("%d", &node->physics_score);
node->next = NULL;
if (head == NULL) {
head = node;
p = node;
} else {
p->next = node;
p = p->next;
}
}
// 输出链表
printf("\n学生的成绩信息如下:\n");
struct student *ptr = head;
while (ptr != NULL) {
printf("姓名:%s\n", ptr->name);
printf("C语言成绩:%d\n", ptr->c_score);
printf("数据库成绩:%d\n", ptr->db_score);
printf("数学成绩:%d\n", ptr->math_score);
printf("物理成绩:%d\n\n", ptr->physics_score);
ptr = ptr->next;
}
// 释放链表内存
ptr = head;
while (ptr != NULL) {
head = head->next;
free(ptr);
ptr = head;
}
return 0;
}
```
上述程序使用了链表来存储学生的成绩信息,每个结点包含了一个学生的四门课程成绩,以及一个指向下一个结点的指针。程序首先创建链表,然后输出链表中每个结点的值,最后释放链表占用的内存。