用c语言设计学生成绩管理系统要求注释代码的数据结构类型,主要调用函数和程序算法
时间: 2024-06-08 16:10:42 浏览: 11
以下是一个用C语言设计的学生成绩管理系统的代码,注释里会说明数据结构类型、主要调用函数和程序算法:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义学生结构体
typedef struct student {
char name[20]; // 学生姓名
int id; // 学生ID
float score; // 学生分数
} Student;
// 定义链表节点结构体
typedef struct node {
Student data; // 节点存储的学生信息
struct node *next; // 指向下一个节点的指针
} Node;
// 新建节点,返回节点指针
Node *create_node(Student data) {
Node *new_node = (Node *) malloc(sizeof(Node));
if (new_node == NULL) {
printf("Memory allocation failed!\n");
exit(1);
}
new_node->data = data;
new_node->next = NULL;
return new_node;
}
// 在链表末尾插入节点
void insert_node(Node **head, Student data) {
Node *new_node = create_node(data);
if (*head == NULL) {
*head = new_node;
} else {
Node *current = *head;
while (current->next != NULL) {
current = current->next;
}
current->next = new_node;
}
}
// 从链表中删除指定姓名的节点
void delete_node(Node **head, char *name) {
Node *current = *head;
Node *previous = NULL;
while (current != NULL) {
if (strcmp(current->data.name, name) == 0) {
if (previous == NULL) {
*head = current->next;
} else {
previous->next = current->next;
}
free(current);
return;
}
previous = current;
current = current->next;
}
}
// 显示链表中所有节点的信息
void display(Node *head) {
printf("Name\t ID\t Score\n");
Node *current = head;
while (current != NULL) {
printf("%s\t %d\t %.1f\n", current->data.name, current->data.id, current->data.score);
current = current->next;
}
}
int main() {
Node *head = NULL; // 链表头节点指针
// 添加学生信息到链表
Student s1 = {"Tom", 1001, 89};
Student s2 = {"Jerry", 1002, 92};
Student s3 = {"Bob", 1003, 85};
insert_node(&head, s1);
insert_node(&head, s2);
insert_node(&head, s3);
// 显示链表中所有节点的信息
display(head);
// 删除指定姓名的节点
delete_node(&head, "Jerry");
// 显示链表中所有节点的信息
display(head);
return 0;
}
```
数据结构类型:
- `Student`:学生结构体,包含姓名、ID和分数。
- `Node`:链表节点结构体,包含数据和指向下一个节点的指针。
主要调用函数:
- `create_node`:新建节点,返回节点指针。
- `insert_node`:在链表末尾插入节点。
- `delete_node`:从链表中删除指定姓名的节点。
- `display`:显示链表中所有节点的信息。
程序算法:
- 创建链表头节点指针。
- 添加学生信息到链表。
- 显示链表中所有节点的信息。
- 删除指定姓名的节点。
- 显示链表中所有节点的信息。
相关推荐
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)