c++实现利用链表实现一个简单的学生信息管理系统: (1)学生信息包括学号、姓名、性别、年龄、电话号码、所学专业、班级等 (2)系统的主要功能包括:学生信息的创建、输出学生信息、查询学生信息、增加学生信息、删除
时间: 2024-10-23 16:15:07 浏览: 69
在C++中,我们可以使用结构体(struct)来表示学生信息,并利用链表(如单链表)来管理这些数据。首先,我们定义一个Student节点结构:
```cpp
struct Student {
int studentID;
string name;
char gender;
int age;
string phoneNumber;
string major;
string classID;
// 构造函数
Student(int id, string n, char g, int a, string p, string m, string cid)
: studentID(id), name(n), gender(g), age(a), phoneNumber(p), major(m), classID(cid) {}
};
```
接下来,我们创建一个链表来存储学生的列表,并提供基本的操作方法:
```cpp
class StudentManagementSystem {
private:
struct Node {
Student data;
Node* next;
};
Node* head;
public:
// 构造函数
StudentManagementSystem() { head = nullptr; }
// 添加新学生
void addStudent(Student newStudent) {
Node* newNode = new Node();
newNode->data = newStudent;
newNode->next = head;
head = newNode;
}
// 删除学生
bool removeStudent(int studentID) {
if (head == nullptr) return false;
if (head->data.studentID == studentID) {
Node* temp = head;
head = head->next;
delete temp;
return true;
} else {
Node* current = head;
while (current->next != nullptr && current->next->data.studentID != studentID) {
current = current->next;
}
if (current->next == nullptr) return false;
Node* temp = current->next;
current->next = current->next->next;
delete temp;
return true;
}
}
// 输出所有学生信息
void displayStudents() {
if (head == nullptr) {
cout << "No students found.\n";
} else {
Node* temp = head;
while (temp != nullptr) {
cout << "Student ID: " << temp->data.studentID << ", Name: " << temp->data.name << ", ..." << endl;
temp = temp->next;
}
}
}
// 查询学生信息
bool findStudent(int studentID) const {
Node* current = head;
while (current != nullptr) {
if (current->data.studentID == studentID) return true;
current = current->next;
}
return false;
}
};
```
阅读全文