数据结构基于线性结构的学生信息存储和查找的实现(数据集自行设计)
时间: 2024-02-18 10:05:57 浏览: 32
可以使用线性结构中的数组或链表来存储学生信息,这里以链表为例。
定义一个学生信息结构体,包含学生的姓名、学号、年龄、性别等基本信息,以及其他需要存储的信息字段。
```c++
struct Student {
string name;
string id;
int age;
char gender;
// 其他信息
};
```
定义一个链表节点结构体,包含学生信息和指向下一个节点的指针。
```c++
struct ListNode {
Student data;
ListNode* next;
};
```
定义一个链表类,包含链表头指针和相关操作方法。
```c++
class LinkedList {
public:
LinkedList() {
head = new ListNode;
head->next = nullptr;
}
~LinkedList() {
ListNode* p = head;
while (p != nullptr) {
ListNode* q = p;
p = p->next;
delete q;
}
}
void insert(Student s) {
ListNode* p = new ListNode;
p->data = s;
p->next = head->next;
head->next = p;
}
bool remove(string id) {
ListNode* p = head;
while (p->next != nullptr) {
if (p->next->data.id == id) {
ListNode* q = p->next;
p->next = q->next;
delete q;
return true;
}
p = p->next;
}
return false;
}
Student* find(string id) {
ListNode* p = head->next;
while (p != nullptr) {
if (p->data.id == id) {
return &p->data;
}
p = p->next;
}
return nullptr;
}
private:
ListNode* head;
};
```
使用链表存储学生信息,并进行插入、删除、查找等操作。
```c++
int main() {
LinkedList list;
Student s1 = {"Tom", "1001", 18, 'M'};
Student s2 = {"Lucy", "1002", 19, 'F'};
list.insert(s1);
list.insert(s2);
Student* p = list.find("1001");
if (p != nullptr) {
cout << "Name: " << p->name << endl;
cout << "Age: " << p->age << endl;
cout << "Gender: " << p->gender << endl;
}
list.remove("1002");
return 0;
}
```
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)