C语言实现:单链表存储学生信息、插入、排序与逆序操作
```c #include <stdio.h> #include <stdlib.h> #include <string.h> // 学生信息结构体 typedef struct Student { int num; char name[20]; int score; struct Student *next; } Student; // 创建并初始化学生信息 Student* createStudent(int num, char name[], int score) { Student *student = (Student*)malloc(sizeof(Student)); student->num = num; strcpy(student->name, name); student->score = score; student->next = NULL; return student; } // 插入学生信息到链表 void insertStudent(Student *head, int num, char name[], int score) { Student *student = createStudent(num, name, score); student->next = head->next; head->next = student; } // 输出学生信息 void printStudent(Student *head) { Student *student = head->next; while(student != NULL) { printf("%d %s %d\n", student->num, student->name, student->score); student = student->next; } } // 排序学生信息(升序) void sortStudent(Student *head) { Student *p, *q; int tempNum, tempScore; char tempName[20]; for (p = head->next; p != NULL; p = p->next) { for (q = p->next; q != NULL; q = q->next) { if (p->score > q->score) { tempNum = p->num; strcpy(tempName, p->name); tempScore = p->score; p->num = q->num; strcpy(p->name, q->name); p->score = q->score; q->num = tempNum; strcpy(q->name, tempName); q->score = tempScore; } } } } // 逆序学生信息 void reverseStudent(Student *head) { Student *p, *q; Student *temp = NULL; p = head->next; while (p != NULL) { q = p->next; p->next = temp; temp = p; p = q; } head->next = temp; } int main() { // 创建头节点 Student *head = (Student*)malloc(sizeof(Student)); head->next = NULL; // 插入学生信息 insertStudent(head, 1, "王令", 98); insertStudent(head, 3, "张成", 67); insertStudent(head, 5, "陈晨", 74); insertStudent(head, 7, "王家", 82); insertStudent(head, 2, "张都", 68); insertStudent(head, 9, "万洪", 73); // 插入新学生信息 insertStudent(head, 12, "林嘉", 84); // 排序学生信息 sortStudent(head); // 输出排序后的学生信息 printf("排序后的学生信息(按分数升序):\n"); printStudent(head); // 逆序学生信息 reverseStudent(head); // 输出逆序后的学生信息 printf("\n逆序后的学生信息(按分数降序):\n"); printStudent(head); return 0; } ``` 在这个实现中,我并没有检查内存分配是否成功。在实际生产代码中,你需要考虑添加相关的错误检查和处理。 ######[AI写代码神器 | 1853点数解答 | 2024-07-21 10:58:49]
- Python实现Q老师成绩管理任务:修改与查看学生成绩(字节豆包 | 191点数解答 | 2025-12-20 20:55:41)98
- Python 代码助力 Q 老师高效管理班级学生成绩:修改与查看一键搞定(字节豆包 | 326点数解答 | 2025-12-27 20:48:12)104
- 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
- C#工程师必知:数组、链表、哈希、队列、栈数据结构优缺点大揭秘! (百度文心 | 561点数解答 | 2023-11-09 17:56:30)275
- Java 实现仿 Windows 资源管理器:文件管理器功能全解析与代码示例(字节豆包 | 623点数解答 | 2025-01-01 14:59:04)268
- Java程序模拟教学选课:林老师开课,58 位学生选课男女比例大揭秘( | 2653点数解答 | 2024-04-02 11:00:16)484
- Java程序设计课程:模拟教学管理,统计58位选课学生男女比重( | 363点数解答 | 2024-04-05 18:31:39)489