下面是一个使用C语言实现的简单的学生信息示意性检索程序,它使用链表来存储学生信息,并实现了您所描述的功能模块。
首先,我们定义学生的信息结构体和链表节点结构体:
```c
#include
#include
#include
// 学生信息结构体
typedef struct Student {
int id;
char sex;
int age;
char class[30];
} Student;
// 链表节点结构体
typedef struct StudentNode {
Student data;
struct StudentNode *next;
} StudentNode;
// 初始化链表
void InitList(StudentNode **head) {
*head = NULL;
}
// 向链表中插入学生信息
void InsertStudent(StudentNode **head, Student stu) {
StudentNode *newNode = (StudentNode *)malloc(sizeof(StudentNode));
if (!newNode) {
printf("Memory allocation failed.\n");
return;
}
newNode->data = stu;
newNode->next = *head;
*head = newNode;
}
// 按学号删除学生信息
void DeleteStudent(StudentNode **head, int id) {
StudentNode *prev = NULL;
StudentNode *current = *head;
while (current && current->data.id != id) {
prev = current;
current = current->next;
}
if (!current) {
printf("Student not found.\n");
return;
}
if (prev) {
prev->next = current->next;
} else {
*head = current->next;
}
free(current);
}
// 修改学生信息
void UpdateStudent(StudentNode **head, int id, Student newStu) {
StudentNode *current = *head;
while (current && current->data.id != id) {
current = current->next;
}
if (!current) {
printf("Student not found.\n");
return;
}
current->data = newStu;
}
// 查找学生信息
StudentNode* FindStudent(StudentNode *head, int id) {
StudentNode *current = head;
while (current && current->data.id != id) {
current = current->next;
}
return current;
}
// 显示所有学生信息
void DisplayStudents(StudentNode *head) {
StudentNode *current = head;
while (current) {
printf("ID: %d, Sex: %c, Age: %d, Class: %s\n",
current->data.id, current->data.sex, current->data.age, current->data.class);
current = current->next;
}
}
// 主函数
int main() {
StudentNode *head = NULL;
int choice, numStudents, id;
Student stu, newStu;
// 主循环
do {
printf("\nStudent Information System\n");
printf("1. Initialize List\n");
printf("2. Insert Student\n");
printf("3. Delete Student\n");
printf("4. Update Student\n");
printf("5. Find Student\n");
printf("6. Display Students\n");
printf("7. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: // 初始化链表
InitList(&head);
break;
case 2: // 插入学生信息
printf("Enter student details (ID, Sex, Age, Class): ");
scanf("%d %c %d %s", &stu.id, &stu.sex, &stu.age, stu.class);
InsertStudent(&head, stu);
break;
case 3: // 删除学生信息
printf("Enter student ID to delete: ");
scanf("%d", &id);
DeleteStudent(&head, id);
break;
case 4: // 修改学生信息
printf("Enter student ID to update: ");
scanf("%d", &id);
printf("Enter new student details (ID, Sex, Age, Class): ");
scanf("%d %c %d %s", &newStu