C语言单链表实现学生信息管理系统
#include <stdio.h>\n#include <stdlib.h>\n\n// 学生信息结构体\ntypedef struct student {\n int number;\n char name[20];\n int age;\n char major[20];\n struct student *next;\n} Student;\n\n// 初始化链表\nStudent *initList() {\n Student *head = (Student *)malloc(sizeof(Student));\n head->next = NULL;\n return head;\n}\n\n// 插入学生信息\nvoid insert(Student *head, int number, char name[20], int age, char major[20]) {\n Student *node = (Student *)malloc(sizeof(Student));\n node->number = number;\n strcpy(node->name, name);\n node->age = age;\n strcpy(node->major, major);\n node->next = head->next;\n head->next = node;\n}\n\n// 输出学生信息\nvoid display(Student *head) {\n Student *node = head->next;\n while (node != NULL) {\n printf("%d %s %d %s\n", node->number, node->name, node->age, node->major);\n node = node->next;\n }\n}\n\n// 输出第五个位置的学生信息\nvoid displayFifth(Student *head) {\n Student *node = head->next;\n int count = 1;\n while (node != NULL && count < 5) {\n node = node->next;\n count++;\n }\n if (node != NULL) {\n printf("第五个位置的学生信息:%d %s %d %s\n", node->number, node->name, node->age, node->major);\n } else {\n printf("链表长度不足5个节点\n");\n }\n}\n\n// 查找第7个结点的直接前驱和后继\nvoid findPreAndSuc(Student *head) {\n Student *node = head->next;\n int count = 1;\n while (node != NULL && count < 7) {\n node = node->next;\n count++;\n }\n if (node != NULL && node->next != NULL) {\n printf("第7个结点的直接前驱:%d %s %d %s\n", node->number, node->name, node->age, node->major);\n printf("第7个结点的直接后继:%d %s %d %s\n", node->next->number, node->next->name, node->next->age, node->next->major);\n } else {\n printf("链表长度不足7个节点或第7个结点无直接后继\n");\n }\n}\n\n// 在第7个节点后插入新的学生信息\nvoid insertAfter7th(Student *head, int number, char name[20], int age, char major[20]) {\n Student *node = head->next;\n int count = 1;\n while (node != NULL && count < 7) {\n node = node->next;\n count++;\n }\n if (node != NULL) {\n Student *newNode = (Student *)malloc(sizeof(Student));\n newNode->number = number;\n strcpy(newNode->name, name);\n newNode->age = age;\n strcpy(newNode->major, major);\n newNode->next = node->next;\n node->next = newNode;\n } else {\n printf("链表长度不足7个节点\n");\n }\n}\n\n// 删除年龄最大者学生基本信息并输出\nvoid deleteMaxAge(Student *head) {\n Student *prev = head;\n Student *node = head->next;\n Student *maxPrev = NULL;\n Student *maxNode = NULL;\n int maxAge = 0;\n while (node != NULL) {\n if (node->age > maxAge) {\n maxAge = node->age;\n maxPrev = prev;\n maxNode = node;\n }\n prev = node;\n node = node->next;\n }\n if (maxNode != NULL) {\n maxPrev->next = maxNode->next;\n free(maxNode);\n }\n}\n\nint main() {\n Student *head = initList();\n insert(head, 2021190001, "赵青", 19, "智科");\n insert(head, 2021190002, "李华", 18, "大数据");\n insert(head, 2021190003, "黎明", 20, "智科");\n insert(head, 2021190004, "向丽", 17, "空信");\n insert(head, 2021190005, "杨晨", 18, "物联网");\n insert(head, 2021190006, "周强", 18, "空信");\n insert(head, 2021119007, "刘帅", 22, "智科");\n insert(head, 2021119009, "李荣", 18, "大数据");\n insert(head, 2021119010, "伍柏", 18, "大数据");\n insert(head, 2021119011, "朴树", 19, "物联网");\n insert(head, 2021119012, "杨颖", 9, "智科");\n\n printf("初始学生信息:\n");\n display(head);\n\n printf("输出第五个位置的学生信息:\n");\n displayFifth(head);\n\n printf("查找第7个结点的直接前驱和后继:\n");\n findPreAndSuc(head);\n\n printf("在第7个节点后插入新的学生信息:\n");\n insertAfter7th(head, 2021119008, "王源", 19, "智科");\n display(head);\n\n printf("删除年龄最大者学生基本信息并输出:\n");\n deleteMaxAge(head);\n display(head);\n\n return 0;\n
原文地址: https://www.cveoy.top/t/topic/poKl 著作权归作者所有。请勿转载和采集!