C++用单链表为某个单位建立一个员工通讯录管理系统,可以方便查询每一个员工的手机号、及电子邮箱。其功能包括通讯录链表的建立、员工通讯信息的查询、修改、插入与删除、以及整个通讯录表的输出。 操作分为查询、修改、添加、删除、打印 1. 查询:输入员工姓名,输出员工信息,若员工不存在,输出“NOT FOUND!” 2. 修改:输入员工姓名、要修改的属性,修改信息。姓名为name、电话为tel、邮箱为mail。 3. 添加:输入员工编号、姓名、电话号码、邮箱。 4. 删除:输入员工姓名,将其从通讯录中删除。 5. 打印:输出通讯录。代码
时间: 2023-07-10 08:05:13 浏览: 290
基于C++语言和单链表数据结构实现的通讯录管理系统,包含文件读取、文件保存、增删改查、排序、清空等功能.zip
5星 · 资源好评率100%
下面是一个使用单链表实现员工通讯录管理系统的C++代码:
```c++
#include <iostream>
#include <string>
using namespace std;
// 定义员工结构体
struct Employee {
int id; // 员工编号
string name; // 员工姓名
string tel; // 员工电话号码
string mail; // 员工电子邮箱
Employee* next; // 指向下一个员工的指针
};
// 建立员工通讯录链表
Employee* createList() {
Employee* head = new Employee; // 头结点不存储有效数据
head->next = NULL;
return head;
}
// 在链表末尾添加员工信息
void addEmployee(Employee* head, int id, string name, string tel, string mail) {
Employee* p = head;
while (p->next != NULL) {
p = p->next; // 找到链表末尾
}
Employee* newEmployee = new Employee;
newEmployee->id = id;
newEmployee->name = name;
newEmployee->tel = tel;
newEmployee->mail = mail;
newEmployee->next = NULL;
p->next = newEmployee; // 将新员工信息添加到链表末尾
}
// 根据员工姓名查询员工信息
void queryEmployee(Employee* head, string name) {
Employee* p = head->next;
while (p != NULL) {
if (p->name == name) {
cout << "ID: " << p->id << endl;
cout << "Name: " << p->name << endl;
cout << "Tel: " << p->tel << endl;
cout << "Mail: " << p->mail << endl;
return;
}
p = p->next;
}
cout << "NOT FOUND!" << endl;
}
// 根据员工姓名修改员工信息
void modifyEmployee(Employee* head, string name, string property, string value) {
Employee* p = head->next;
while (p != NULL) {
if (p->name == name) {
if (property == "name") {
p->name = value;
} else if (property == "tel") {
p->tel = value;
} else if (property == "mail") {
p->mail = value;
} else {
cout << "INVALID PROPERTY!" << endl;
return;
}
cout << "MODIFY SUCCESSFULLY!" << endl;
return;
}
p = p->next;
}
cout << "NOT FOUND!" << endl;
}
// 根据员工姓名删除员工信息
void deleteEmployee(Employee* head, string name) {
Employee* p = head->next;
Employee* pre = head;
while (p != NULL) {
if (p->name == name) {
pre->next = p->next;
delete p;
cout << "DELETE SUCCESSFULLY!" << endl;
return;
}
pre = p;
p = p->next;
}
cout << "NOT FOUND!" << endl;
}
// 输出员工通讯录信息
void printList(Employee* head) {
Employee* p = head->next;
while (p != NULL) {
cout << "ID: " << p->id << endl;
cout << "Name: " << p->name << endl;
cout << "Tel: " << p->tel << endl;
cout << "Mail: " << p->mail << endl;
p = p->next;
}
}
int main() {
Employee* head = createList(); // 建立员工通讯录链表
while (true) {
cout << "1. Query employee" << endl;
cout << "2. Modify employee" << endl;
cout << "3. Add employee" << endl;
cout << "4. Delete employee" << endl;
cout << "5. Print list" << endl;
cout << "0. Exit" << endl;
int choice;
cin >> choice;
switch (choice) {
case 1: {
string name;
cout << "Input employee name: ";
cin >> name;
queryEmployee(head, name);
break;
}
case 2: {
string name, property, value;
cout << "Input employee name: ";
cin >> name;
cout << "Input property(name/tel/mail): ";
cin >> property;
cout << "Input new value: ";
cin >> value;
modifyEmployee(head, name, property, value);
break;
}
case 3: {
int id;
string name, tel, mail;
cout << "Input employee id: ";
cin >> id;
cout << "Input employee name: ";
cin >> name;
cout << "Input employee tel: ";
cin >> tel;
cout << "Input employee mail: ";
cin >> mail;
addEmployee(head, id, name, tel, mail);
break;
}
case 4: {
string name;
cout << "Input employee name: ";
cin >> name;
deleteEmployee(head, name);
break;
}
case 5: {
printList(head);
break;
}
case 0: {
return 0;
}
default: {
cout << "INVALID CHOICE!" << endl;
break;
}
}
}
return 0;
}
```
注意,上述代码中的员工通讯录链表使用头结点,这是一种常见的链表实现方式。头结点不存储有效数据,只用于指向链表的第一个节点。另外,为了简化代码,我们假设员工姓名是唯一的,因此在查询、修改和删除员工信息时,只需要根据员工姓名来操作即可。
阅读全文