学生考勤表的项目, 考勤表需要包括姓名,座号,手机3个数据成员。 单链表实现
时间: 2023-06-01 07:04:01 浏览: 129
以下是一个基于单链表的学生考勤表的实现:
```C++
#include <iostream>
#include <string>
using namespace std;
// 定义学生结构体
struct Student {
string name; // 姓名
int seat; // 座号
string phone; // 手机号
Student* next; // 指向下一个节点的指针
};
// 定义链表类
class AttendanceList {
public:
AttendanceList() {
head = nullptr;
}
// 添加学生
void addStudent(string name, int seat, string phone) {
Student* newStudent = new Student{ name, seat, phone, nullptr };
if (head == nullptr) {
head = newStudent;
}
else {
Student* current = head;
while (current->next != nullptr) {
current = current->next;
}
current->next = newStudent;
}
}
// 删除学生
void removeStudent(int seat) {
if (head == nullptr) {
return;
}
if (head->seat == seat) {
Student* temp = head;
head = head->next;
delete temp;
return;
}
Student* current = head;
while (current->next != nullptr && current->next->seat != seat) {
current = current->next;
}
if (current->next != nullptr) {
Student* temp = current->next;
current->next = temp->next;
delete temp;
}
}
// 显示所有学生信息
void display() {
Student* current = head;
while (current != nullptr) {
cout << "Name: " << current->name << ", Seat: " << current->seat << ", Phone: " << current->phone << endl;
current = current->next;
}
}
private:
Student* head;
};
// 测试
int main() {
AttendanceList list;
list.addStudent("张三", 1, "12345678901");
list.addStudent("李四", 2, "12345678902");
list.addStudent("王五", 3, "12345678903");
list.display();
list.removeStudent(2);
list.display();
return 0;
}
```
以上代码中,`Student`结构体定义了学生的姓名、座号和手机号码,以及指向下一个节点的指针。`AttendanceList`类定义了一个链表,其中包括添加学生、删除学生和显示所有学生信息等方法。在`addStudent`方法中,我们首先创建一个新的`Student`节点,然后遍历链表找到最后一个节点,将新节点添加到链表末尾。在`removeStudent`方法中,我们先判断链表是否为空,然后判断要删除的节点是否为头节点,否则遍历链表找到要删除的节点并将其从链表中移除。在测试代码中,我们先添加了三个学生,然后显示所有学生信息,接着删除了一个学生,再次显示所有学生信息。
阅读全文
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)