下面是一个使用C语言实现的学生信息示意性检索程序的示例。这个程序包含了你所提到的六个功能模块:初始化、插入、删除、修改、查找和显示。请注意,这个程序是一个简化版本,仅用于演示链表操作,并未包含错误处理或复杂的边界检查。 ```c #include #include #include // 学生信息结构体 typedef struct Student { int id; char sex; int age; char class[30]; struct Student *next; } Student; // 函数声明 Student* createStudent(); void insertStudent(Student **head, Student *newStudent); void deleteStudent(Student **head, int id); void modifyStudent(Student **head, int id); void findStudent(Student *head, int id); void displayStudents(Student *head); void freeStudents(Student *head); int main() { int choice, numStudents, id; Student *head = NULL; // 循环处理用户请求 do { printf("\n学生信息管理程序\n"); printf("1. 初始化\n"); printf("2. 插入学生\n"); printf("3. 删除学生\n"); printf("4. 修改学生信息\n"); printf("5. 查找学生信息\n"); printf("6. 显示所有学生信息\n"); printf("7. 退出\n"); printf("请选择操作:"); scanf("%d", &choice); switch (choice) { case 1: printf("请输入学生人数:"); scanf("%d", &numStudents); for (int i = 0; i < numStudents; i++) { Student *newStudent = createStudent(); insertStudent(&head, newStudent); } break; case 2: { Student *newStudent = createStudent(); insertStudent(&head, newStudent); } break; case 3: printf("请输入要删除学生的学号:"); scanf("%d", &id); deleteStudent(&head, id); break; case 4: printf("请输入要修改信息学生的学号:"); scanf("%d", &id); modifyStudent(&head, id); break; case 5: printf("请输入要查找学生的学号:"); scanf("%d", &id); findStudent(head, id); break; case 6: displayStudents(head); break; case 7: printf("程序结束\n"); freeStudents(head); break; default: printf("无效选择,请重新输入\n"); } } while (choice != 7); return 0; } Student* createStudent() { Student *newStudent = (Student*)malloc(sizeof(Student)); if (!newStudent) { perror("Error allocating memory for new student"); exit(EXIT_FAILURE); } printf("请输入学号:"); scanf("%d", &newStudent->id); printf("请输入性别(M/F):"); scanf(" %c", &newStudent->sex); printf("请输入年龄:"); scanf("%d", &newStudent->age); printf("请输入班级:"); scanf(" %s", newStudent->class); newStudent->next = NULL; return newStudent; } void insertStudent(Student **head, Student *newStudent) { newStudent->next = *head; *head = newStudent; } void deleteStudent(Student **head, int id) { Student *current = *head; Student *prev = NULL; while (current != NULL && current->id != id) { prev = current; current = current->next; } if (current == NULL) { printf("未找到学号为 %d 的学生\n", id); } else { if (prev == NULL) { *head = current->next; } else { prev->next = current->next; } free(current); printf("学生删除成功\n"); }

点赞(0)
×
关注公众号,登录后继续创作
或点击进入高级版AI
扫码关注后未收到验证码,回复【登录】二字获取验证码
发表
评论
返回
顶部