用链表一个学生信息管理系统
时间: 2023-10-23 09:13:08 浏览: 51
好的,下面是一个简单的学生信息管理系统,使用链表来存储学生信息:
```c++
#include <iostream>
#include <string>
using namespace std;
// 学生结构体
struct Student {
string name;
int age;
float score;
Student* next;
};
// 添加学生信息
void addStudent(Student*& head, string name, int age, float score) {
Student* newStudent = new Student;
newStudent->name = name;
newStudent->age = age;
newStudent->score = score;
newStudent->next = NULL;
if (head == NULL) {
head = newStudent;
} else {
Student* current = head;
while (current->next != NULL) {
current = current->next;
}
current->next = newStudent;
}
}
// 删除学生信息
void deleteStudent(Student*& head, string name) {
if (head == NULL) {
cout << "链表为空,无法删除学生信息" << endl;
return;
}
if (head->name == name) {
Student* temp = head;
head = head->next;
delete temp;
return;
}
Student* current = head;
Student* previous = NULL;
while (current != NULL && current->name != name) {
previous = current;
current = current->next;
}
if (current == NULL) {
cout << "未找到要删除的学生信息" << endl;
return;
}
previous->next = current->next;
delete current;
}
// 显示学生信息
void showStudents(Student* head) {
if (head == NULL) {
cout << "链表为空,无法显示学生信息" << endl;
return;
}
Student* current = head;
while (current != NULL) {
cout << "姓名:" << current->name << ",年龄:" << current->age << ",成绩:" << current->score << endl;
current = current->next;
}
}
int main() {
// 初始化链表头指针
Student* head = NULL;
// 添加学生信息
addStudent(head, "张三", 20, 85.5);
addStudent(head, "李四", 21, 90.0);
addStudent(head, "王五", 19, 78.5);
// 显示学生信息
showStudents(head);
// 删除学生信息
deleteStudent(head, "李四");
// 显示学生信息
showStudents(head);
return 0;
}
```
以上代码实现了一个简单的学生信息管理系统,使用链表来存储学生信息。在这个系统中,每个学生都被表示为一个结构体,其中包含姓名、年龄和成绩等信息,同时还有一个指向下一个学生的指针。通过链表,可以方便地添加、删除和显示学生信息。