酷代码 AI
菜单
全部AI  /  热门  /  AI写代码神器
服务商

现在有一个班的学生,学生包括学号、姓名、数据结构分数 1、建立一个带头节点的单链表存储结构存储学生的信息 初始包含: 学号 姓名 分数 1 王令 98 3 张成 67 5 陈晨 74 7 王家 82 2 张都 68 9 万洪 73 2、在单链表中插入学号为12,姓名为林嘉,数据结构为84的学生信息 3、对学生信息进行排序,按照分数从小到大的顺序进行排序,并按排序号的顺序进行输出 4、在3的基础上对学生信息进行逆序操作,也就是让学生分数从大到小存储在单链表中,并输出排序后的结果 用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; } ``` 在这个实现中,我并没有检查内存分配是否成功。在实际生产代码中,你需要考虑添加相关的错误检查和处理。 [2024-07-21 10:58:49 | AI写代码神器 | 1853点数解答]

相关提问