C++用单链表为某个单位建立一个员工通讯录管理系统,可以方便查询每一个员工的手机号、及电子邮箱。其功能包括通讯录链表的建立、员工通讯信息的查询、修改、插入与删除、以及整个通讯录表的输出。 操作分为查询、修改、添加、删除、打印 1. 查询:输入员工姓名,输出员工信息,若员工不存在,输出“NOT FOUND!” 2. 修改:输入员工姓名、要修改的属性,修改信息。姓名为name、电话为tel、邮箱为mail。 3. 添加:输入员工编号、姓名、电话号码、邮箱。 4. 删除:输入员工姓名,将其从通讯录中删除。 5. 打印:输出通讯录。用到存储结构: typedef struct { /*员工通讯信息的结构类型定义*/ char num[5]; /*员工编号*/ char name[10]; /*员工姓名*/ char tel[15]; /*办公室电话号码*/ char call[15]; /*手机号码*/ char mail[25]; /*邮箱*/ }DataType; /*通讯录单链表的结点类型*/ typedef struct node { DataType data; /*结点的数据域*/ struct node *next; /*结点的指针域*/ }ListNode,*LinkList;代码
时间: 2023-12-03 18:47:45 浏览: 100
以下是基本实现代码,可以根据需要进行修改和完善:
```c++
#include <iostream>
#include <cstring>
using namespace std;
typedef struct {
char num[5];
char name[10];
char tel[15];
char call[15];
char mail[25];
} DataType;
typedef struct node {
DataType data;
struct node *next;
} ListNode, *LinkList;
// 创建通讯录链表
LinkList createList() {
LinkList head = new ListNode;
head->next = NULL;
return head;
}
// 添加员工信息
void insertNode(LinkList L, DataType data) {
ListNode *p = new ListNode;
p->data = data;
p->next = L->next;
L->next = p;
}
// 查询员工信息
void searchNode(LinkList L, char name[]) {
ListNode *p = L->next;
while (p != NULL) {
if (strcmp(p->data.name, name) == 0) {
cout << "编号:" << p->data.num << endl;
cout << "姓名:" << p->data.name << endl;
cout << "电话:" << p->data.tel << endl;
cout << "手机:" << p->data.call << endl;
cout << "邮箱:" << p->data.mail << endl;
return;
}
p = p->next;
}
cout << "NOT FOUND!" << endl;
}
// 修改员工信息
void modifyNode(LinkList L, char name[], char field[], char value[]) {
ListNode *p = L->next;
while (p != NULL) {
if (strcmp(p->data.name, name) == 0) {
if (strcmp(field, "name") == 0) {
strcpy(p->data.name, value);
} else if (strcmp(field, "tel") == 0) {
strcpy(p->data.tel, value);
} else if (strcmp(field, "call") == 0) {
strcpy(p->data.call, value);
} else if (strcmp(field, "mail") == 0) {
strcpy(p->data.mail, value);
} else {
cout << "Invalid field!" << endl;
return;
}
cout << "Modify succeed!" << endl;
return;
}
p = p->next;
}
cout << "NOT FOUND!" << endl;
}
// 删除员工信息
void deleteNode(LinkList L, char name[]) {
ListNode *p = L->next, *pre = L;
while (p != NULL) {
if (strcmp(p->data.name, name) == 0) {
pre->next = p->next;
delete(p);
cout << "Delete succeed!" << endl;
return;
}
pre = p;
p = p->next;
}
cout << "NOT FOUND!" << endl;
}
// 输出通讯录
void printList(LinkList L) {
ListNode *p = L->next;
while (p != NULL) {
cout << "编号:" << p->data.num << endl;
cout << "姓名:" << p->data.name << endl;
cout << "电话:" << p->data.tel << endl;
cout << "手机:" << p->data.call << endl;
cout << "邮箱:" << p->data.mail << endl;
p = p->next;
}
}
int main() {
LinkList L = createList();
int choice;
char name[10];
char field[10];
char value[25];
DataType data;
while (true) {
cout << "1.查询 2.修改 3.添加 4.删除 5.打印 0.退出" << endl;
cin >> choice;
if (choice == 0) {
break;
}
switch (choice) {
case 1:
cout << "请输入员工姓名:";
cin >> name;
searchNode(L, name);
break;
case 2:
cout << "请输入员工姓名:";
cin >> name;
cout << "请输入要修改的属性(name/tel/call/mail):";
cin >> field;
cout << "请输入新值:";
cin >> value;
modifyNode(L, name, field, value);
break;
case 3:
cout << "请输入员工编号、姓名、电话、手机、邮箱:";
cin >> data.num >> data.name >> data.tel >> data.call >> data.mail;
insertNode(L, data);
cout << "Add succeed!" << endl;
break;
case 4:
cout << "请输入员工姓名:";
cin >> name;
deleteNode(L, name);
break;
case 5:
printList(L);
break;
default:
cout << "Invalid choice!" << endl;
break;
}
}
return 0;
}
```
阅读全文