以下是一个简单的C语言程序,用于实现你所描述的学生信息示意性检索程序。这个程序包括六个功能模块:初始化(创建链表)、插入学生、删除学生、修改学生信息、查找学生信息以及显示所有学生信息。
```c
#include
#include
#include
// 学生信息结构体
typedef struct Student {
int id;
char sex;
int age;
char class[30];
struct Student *next;
} Student;
// 函数声明
void initStudents(Student **head);
void insertStudent(Student **head);
void deleteStudent(Student **head);
void modifyStudent(Student **head);
void findStudent(Student *head);
void displayStudents(Student *head);
void freeStudents(Student *head);
int main() {
int option, numStudents;
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", &option);
switch (option) {
case 1:
initStudents(&head);
break;
case 2:
insertStudent(&head);
break;
case 3:
deleteStudent(&head);
break;
case 4:
modifyStudent(&head);
break;
case 5:
findStudent(head);
break;
case 6:
displayStudents(head);
break;
case 7:
printf("程序退出...\n");
freeStudents(head);
break;
default:
printf("无效选择,请重新输入!\n");
}
} while (option != 7);
return 0;
}
void initStudents(Student **head) {
int i, num;
printf("请输入班级学生人数:");
scanf("%d", &num);
getchar(); // 清除输入缓冲区中的换行符
for (i = 0; i < num; i++) {
insertStudent(head);
}
}
void insertStudent(Student **head) {
Student *newStudent, *current;
newStudent = (Student *)malloc(sizeof(Student));
if (newStudent == NULL) {
printf("内存分配失败!\n");
return;
}
printf("请输入学生学号:");
scanf("%d", &newStudent->id);
getchar(); // 清除输入缓冲区中的换行符
printf("请输入学生性别:");
scanf("%c", &newStudent->sex);
printf("请输入学生年龄:");
scanf("%d", &newStudent->age);
printf("请输入学生班级:");
fgets(newStudent->class, sizeof(newStudent->class), stdin);
newStudent->class[strcspn(newStudent->class, "\n")] = 0; // 去除fgets读取到的换行符
newStudent->next = NULL;
if (*head == NULL) {
*head = newStudent;
} else {
current = *head;
while (current->next != NULL) {
current = current->next;
}
current->next = newStudent;
}
printf("学生信息添加成功!\n");
}
void deleteStudent(Student **head) {
Student *current, *prev;
int id;
printf("请输入要删除学生的学号:");
scanf("%d", &id);
if (*head == NULL) {
printf("链表为空,无法删除!\n");
return;
}
if ((*head)->id == id) {
current = *head;
*head = current->next;
free(current);
printf("学生信息删除成功!\n");
return;
}
prev = *head;
current = prev->next;
while (current != NULL) {