通讯录信息管理系统链表在输入两个一样的名字时只出现一个
时间: 2024-04-05 07:35:06 浏览: 17
这个问题的原因是在添加新联系人时,没有判断输入的姓名是否已经在链表中存在。因此,当输入两个相同的姓名时,程序会认为这是两个不同的联系人,而实际上它们应该是同一个联系人。
解决这个问题的方法是,在添加新联系人时,遍历整个链表,判断是否存在相同的姓名。如果存在相同的姓名,则不添加新联系人,而是将新的联系号码添加到已存在联系人的号码列表中。如果不存在相同的姓名,则添加新的联系人到链表中。这样就可以保证在输入两个相同的姓名时,只会出现一个联系人的情况。
相关问题
用链表一个学生信息管理系统
好的,下面是一个简单的学生信息管理系统,使用链表来存储学生信息:
```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;
}
```
以上代码实现了一个简单的学生信息管理系统,使用链表来存储学生信息。在这个系统中,每个学生都被表示为一个结构体,其中包含姓名、年龄和成绩等信息,同时还有一个指向下一个学生的指针。通过链表,可以方便地添加、删除和显示学生信息。
用链表写一个人力资源信息管理系统,用在数据结构课程设计
好的,下面是一个简单的链表人力资源信息管理系统的实现。
首先,定义一个员工信息结构体,包含员工的姓名、编号、年龄等信息:
```c++
struct Employee {
string name;
int id;
int age;
// 其他员工信息
};
```
然后,定义一个链表节点结构体,包含员工信息和指向下一个节点的指针:
```c++
struct ListNode {
Employee employee;
ListNode* next;
};
```
接着,定义一个链表类,包含链表的头节点、尾节点和节点数量等信息,以及一些基本的操作函数,例如添加、删除、查找等:
```c++
class EmployeeLinkedList {
public:
EmployeeLinkedList() {
head = nullptr;
tail = nullptr;
size = 0;
}
~EmployeeLinkedList() {
ListNode* current = head;
while(current != nullptr) {
ListNode* next = current->next;
delete current;
current = next;
}
head = nullptr;
tail = nullptr;
size = 0;
}
void addEmployee(const Employee& employee) {
ListNode* newNode = new ListNode;
newNode->employee = employee;
newNode->next = nullptr;
if(head == nullptr) {
head = newNode;
tail = newNode;
} else {
tail->next = newNode;
tail = newNode;
}
size++;
}
void deleteEmployee(int id) {
ListNode* current = head;
ListNode* previous = nullptr;
while(current != nullptr && current->employee.id != id) {
previous = current;
current = current->next;
}
if(current != nullptr) {
if(previous == nullptr) {
head = current->next;
} else {
previous->next = current->next;
}
if(current == tail) {
tail = previous;
}
size--;
delete current;
}
}
Employee* findEmployee(int id) {
ListNode* current = head;
while(current != nullptr && current->employee.id != id) {
current = current->next;
}
if(current != nullptr) {
return &(current->employee);
} else {
return nullptr;
}
}
int getSize() const {
return size;
}
private:
ListNode* head;
ListNode* tail;
int size;
};
```
最后,在主函数中可以通过调用链表类的函数来完成员工信息的添加、删除、查找等操作。
这是一个简单的链表人力资源信息管理系统的实现,可以根据具体需要进行修改和完善。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)