用c++编写学生基本信息管理程序。用链表存储一个班的学生信息(最多100位学生),每个学生的信息有:学号,姓名,性别,联系电话和家庭住址。需要实现的功能有:新建学生信息链表,对学生信息进行增加、删除和查找。
时间: 2024-09-20 10:05:22 浏览: 44
在C++中,我们可以使用结构体定义学生的相关信息,并创建一个动态链表的数据结构来存储这100位学生的信息。首先,我们需要定义一个`Student`结构:
```cpp
struct Student {
int student_id;
string name;
char gender;
string phone;
string address;
};
```
接着,我们创建一个`Node`作为链表节点,包含一个`Student`数据和指向下一个节点的指针:
```cpp
class Node {
public:
Student info;
Node* next;
};
```
然后,我们可以实现以下几个功能:
1. **新建链表** (Create a new list):
```cpp
Node* createList() {
return nullptr; // 初始化为空链表
}
```
2. **添加新学生** (Add a new student):
```cpp
void addStudent(Node*& head, const Student& student) {
Node* newNode = new Node();
newNode->info = student;
newNode->next = head;
head = newNode;
}
```
3. **删除学生** (Delete a student by ID or name):
```cpp
bool deleteStudent(Node*& head, int id, const string& name) {
if (!head) return false;
if (head->info.student_id == id || head->info.name == name) {
Node* temp = head;
head = head->next;
delete temp;
return true;
}
for (Node* curr = head; curr->next != nullptr; curr = curr->next) {
if (curr->next->info.student_id == id || curr->next->info.name == name) {
Node* temp = curr->next;
curr->next = curr->next->next;
delete temp;
return true;
}
}
return false;
}
```
4. **查找学生** (Find a student by ID or name):
```cpp
Node* findStudent(const Node* head, int id, const string& name) {
for (Node* curr = head; curr != nullptr; curr = curr->next) {
if (curr->info.student_id == id || curr->info.name == name)
return curr;
}
return nullptr;
}
```
阅读全文
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![text/x-c](https://img-home.csdnimg.cn/images/20250102104920.png)
![](https://img-home.csdnimg.cn/images/20250102104920.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pptx](https://img-home.csdnimg.cn/images/20241231044947.png)