```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; } ``` 在这个实现中,我并没有检查内存分配是否成功。在实际生产代码中,你需要考虑添加相关的错误检查和处理。 [2024-07-21 10:58:49 | AI写代码神器 | 1853点数解答]