C语言实现:用单链表存储学生信息并完成插入、排序与逆序操作
要解决这个问题,首先需要定义一个结构体来存储学生信息,然后使用单链表来存储这些信息。接下来,根据你的需求,我们将逐步实现插入、排序和逆序操作。 步骤1:定义结构体和链表节点 ```c #include <stdio.h> #include <stdlib.h> typedef struct Student { int id; char name[20]; int score; struct Student *next; } Student; Student *head = NULL; // 定义头节点 ``` 步骤2:初始化链表并插入初始学生信息 ```c void insertInitialStudents() { Student *newStudent, *current; newStudent = (Student *)malloc(sizeof(Student)); newStudent->id = 1; strcpy(newStudent->name, "王令"); newStudent->score = 98; newStudent->next = NULL; head = newStudent; current = head; for (int i = 2; i <= 9; i++) { newStudent = (Student *)malloc(sizeof(Student)); switch (i) { case 2: newStudent->id = 3; strcpy(newStudent->name, "张成"); newStudent->score = 67; break; case 3: newStudent->id = 5; strcpy(newStudent->name, "陈晨"); newStudent->score = 74; break; case 4: newStudent->id = 7; strcpy(newStudent->name, "王家"); newStudent->score = 82; break; case 5: newStudent->id = 2; strcpy(newStudent->name, "张都"); newStudent->score = 68; break; case 6: newStudent->id = 9; strcpy(newStudent->name, "万洪"); newStudent->score = 73; break; default: break; } if (i != 9) { newStudent->next = NULL; current->next = newStudent; current = newStudent; } } } ``` 步骤3:插入新学生信息 ```c void insertStudent(int id, char *name, int score) { Student *newStudent = (Student *)malloc(sizeof(Student)); newStudent->id = id; strcpy(newStudent->name, name); newStudent->score = score; newStudent->next = NULL; Student *current = head; while (current->next != NULL) { current = current->next; } current->next = newStudent; } ``` 步骤4:排序并输出 ```c void sortAndPrint() { Student *a, *b, *c; int tempId; char tempName[20]; int tempScore; for (a = head; a != NULL; a = a->next) { for (b = a->next; b != NULL; b = b->next) { if (a->score > b->score) { tempId = a->id; strcpy(tempName, a->name); tempScore = a->score; a->id = b->id; strcpy(a->name, b->name); a->score = b->score; b->id = tempId; strcpy(b->name, tempName); b->score = tempScore; } } } printf("Sorted List:\n"); for (c = head; c != NULL; c = c->next) { printf("%d %s %d\n", c->id, c->name, c->score); } } ``` 步骤5:逆序输出 ```c void reversePrint() { Student *current = head; if (current == NULL || current->next == NULL) return; Student *prev = NULL; Student *next = NULL; while (current != NULL) { next = current->next; current->next = prev; prev = current; current = next; } head = prev; printf("Reversed Sorted List:\n"); for (current = head; current != NULL; current = current->next) { printf("%d %s %d\n", current->id, current->name, current->score); } } int main() { insertInitialStudents(); insertStudent(12, "林嘉", 84); sortAndPrint(); reversePrint(); return 0; } ``` 以上代码实现了从初始化链表、插入新学生信息、排序并输出,再到逆序输出的全部功能。注意,这里的排序是基于分数从小到大的顺序进行的,而逆序输出则是将排序后的链表反转,使得分数从大到小排列。 ######[AI写代码神器 | 1067点数解答 | 2024-07-22 16:25:54]
- Python实现Q老师成绩管理任务:修改与查看学生成绩(字节豆包 | 191点数解答 | 2025-12-20 20:55:41)98
- Python 代码助力 Q 老师高效管理班级学生成绩:修改与查看一键搞定(字节豆包 | 326点数解答 | 2025-12-27 20:48:12)104
- 小核桃破密码遇进度条:补全代码实现百分比加载及完成提示 (字节豆包 | 326点数解答 | 2025-12-13 20:06:10)58
- 小核桃补全代码:实现进度条加载信息输出(字节豆包 | 53点数解答 | 2026-01-10 19:26:25)49
- 小核桃解门密遇进度条,代码补全助其加载完成!(字节豆包 | 90点数解答 | 2026-01-10 19:26:42)47
- 小核桃破解密码遇进度条!C++代码补全助其完成加载(百度文心 | 231点数解答 | 2026-01-10 19:57:04)55
- Java:抽象类 Vehicle 及子类 Car、MotorBike 实战演示(讯飞星火 | 440点数解答 | 2024-06-06 23:18:54)326
- 用 Pandas 深度剖析数据:从读取到统计分析全流程实战(GPT | 642点数解答 | 2024-06-30 15:39:49)375
- 用 Pandas 深入分析招聘数据:从数据读取到可视化全流程解析(GPT | 1539点数解答 | 2024-06-30 15:56:50)387
- Java实现有序集合合并与交集算法(顺序表+单链表)(百度文心 | 1247点数解答 | 2026-03-25 10:35:22)27
- Python 实现球类:精准计算半径、表面积与体积,附输入验证与异常处理!(阿里通义 | 261点数解答 | 2024-11-28 21:19:39)371
- 深入解析格雷码生成算法:递归求解与代码实现(DeepSeek | 443点数解答 | 2026-01-05 12:21:49)69