C语言实现:学生信息单链表的创建、插入、排序与逆序
要解决这个问题,首先需要定义一个结构体来存储学生信息,然后使用单链表来存储这些信息。接下来,根据你的需求,我们将逐步实现每个功能。 1. 定义学生结构体和链表节点 ```c #include <stdio.h> #include <stdlib.h> typedef struct Student { int id; char name[20]; int score; struct Student *next; } Student; // 创建一个新的学生节点 Student* createStudent(int id, const char *name, int score) { Student *newStudent = (Student*)malloc(sizeof(Student)); newStudent->id = id; strcpy(newStudent->name, name); newStudent->score = score; newStudent->next = NULL; return newStudent; } ``` 2. 建立单链表并插入初始学生信息 ```c void insertAtEnd(Student head, int id, const char *name, int score) { Student *newNode = createStudent(id, name, score); if (*head == NULL) { *head = newNode; return; } Student *last = *head; while (last->next != NULL) { last = last->next; } last->next = newNode; } int main() { Student *head = NULL; insertAtEnd(&head, 1, "王令", 98); insertAtEnd(&head, 3, "张成", 67); insertAtEnd(&head, 5, "陈晨", 74); insertAtEnd(&head, 7, "王家", 82); insertAtEnd(&head, 2, "张都", 68); insertAtEnd(&head, 9, "万洪", 73); } ``` 3. 插入新学生信息 ```c void insertNewStudent(Student head, int id, const char *name, int score) { insertAtEnd(head, id, name, score); } // 在主函数中调用 insertNewStudent(&head, 12, "林嘉", 84); ``` 4. 排序并输出 小到大排序 ```c void sortList(Student head) { Student *current = *head; Student *index = NULL; int tempScore; char tempName[20]; int tempId; if (head == NULL) { return; } else { while (current != NULL) { index = current->next; while (index != NULL) { if (current->score > index->score) { tempScore = current->score; current->score = index->score; index->score = tempScore; tempId = current->id; current->id = index->id; index->id = tempId; strcpy(tempName, current->name); strcpy(current->name, index->name); strcpy(index->name, tempName); } index = index->next; } current = current->next; } } } void printList(Student *head) { while (head != NULL) { printf("ID: %d, Name: %s, Score: %d\n", head->id, head->name, head->score); head = head->next; } } ``` 大到小排序 只需在`sortList`函数中的比较条件改为`current->score < index->score`即可。 完整代码 将以上所有部分组合起来,你就可以得到完整的程序。注意在实际使用中,你需要在`main`函数中调用所有这些函数,并且在程序结束时释放所有分配的内存以避免内存泄漏。 ######[AI写代码神器 | 828点数解答 | 2024-07-21 10:54:07]
- 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
- C++实现顺序存储结构基本操作:初始化、插入、删除等全解析(GPT | 1743点数解答 | 2024-09-26 00:20:17)296
- Python 实现球类:精准计算半径、表面积与体积,附输入验证与异常处理!(阿里通义 | 261点数解答 | 2024-11-28 21:19:39)371
- C语言实现多班级学生信息管理系统:含增删改查与多样排序功能(GPT | 2547点数解答 | 2024-12-15 22:08:58)371
- C语言实现学生信息管理系统:含增删改查与多种排序功能(GPT | 6408点数解答 | 2024-12-15 22:10:03)366
- 深入解析格雷码生成算法:递归求解与代码实现(DeepSeek | 443点数解答 | 2026-01-05 12:21:49)69
- Java 创建 HTTP POST 请求实现文件上传:代码示例与注意要点 (字节豆包 | 300点数解答 | 2024-12-06 16:07:20)353
- 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