#include<stdio.h>
#include<stdlib.h>
#include<string.h>
// 创建新学生节点
Student*creat_student(int xuehao,char *name,float score){
Student*new_student=(Student*)malloc(sizeof(Student));
new_student->xuehao=xuehao;
strcpy(new_student->name,name);
new_student->score=score;
new_student->next=NULL;
return new_student;
}
strcpy(new_student->name, name);
new_student->score = score;
new_student->next = NULL;
return new_student;
}
// 向链表中添加学生
void add_student(Student **head, int xuehao, char *name, float score) {
Student *new_student = create_student(xuehao, name, score);
new_student->next = *head;
*head = new_student;
}
// 从链表中删除学生
void delete_student(Student **head, int xuehao) {
Student *temp = *head, *prev;
if (temp != NULL && temp->xuehao == xuehao) {
*head = temp->next;
free(temp);
return;
}
while (temp != NULL && temp->xuehao != xuehao) {
prev = temp;
temp = temp->next;
}
if (temp == NULL) return;
prev->next = temp->next;
free(temp);
}
// 在链表中查找学生
Student *find_student(Student *head, int xuehao) {
Student *current = head;
while (current != NULL) {
if (current->xuehao == xuehao) {
return current;
}
current = current->next;
}
return NULL;
}
// 显示所有学生信息
void display_students(Student *head) {
Student *current = head;
while (current != NULL) {
printf("学号: %d, 名字: %s, 成绩: %.2f", current->xuehao, current->name, current->score);
current = current->next;
}
}
int main() {
Student *head = NULL;
// 添加学生信息
add_student(&head, 1, "张三", 90.0);
add_student(&head, 2, "李四", 80.0);
add_student(&head, 3, "王五", 85.0);
// 显示学生信息
display_students(head);
// 删除学生信息
delete_student(&head, 2);
// 查找学生信息
Student *found_student = find_student(head, 1);
if (found_student != NULL)
{
printf("找到学生: 学号: %d, 名字: %s, 成绩: %.2f", found_student->xuehao, found_student->name, found_student->score);
}
else
{
printf("未找到学生");
}
return 0;
}