一、通讯录的基本功能
1.添加联系人
2.显示联系人
3.查找联系人
4.删除联系人
5.修改联系人
6.清除联系人
7.退出系统
二、分代码
1.主体框架
#include<iostream>
#include<string>
using namespace std;
struct Contacts
{
string name;
string phone;
string address;
};
struct Node
{
Contacts person;
Node* next;
};
void showMenu();
void add(Node*& head);
void show(Node* head);
void search(Node* head);
void del(Node* head);
void revise(Node* head);
void empty(Node* &head);
int main()
{
Node* head = NULL;
while (1)
{
showMenu();//显示菜单
int choice;
cin >> choice;
switch (choice)
{
case 1://添加联系人
add(head);
break;
case 2://显示联系人
show(head);
break;
case 3://查找联系人
search(head);
break;
case 4://删除联系人
del(head);
break;
case 5://修改联系人
revise(head);
break;
case 6://清空联系人
empty(head);
break;
case 7://退出系统
system("cls");
cout << "欢迎下次使用!\n";
return 0;
}
}
return 0;
}
2.显示菜单
void showMenu()
{
string str1 = "*****";
string str2 = "**********************";
cout << str2 << endl;
cout << str1 << "1.添加联系人" << str1 << endl;
cout << str1 << "2.显示联系人" << str1 << endl;
cout << str1 << "3.查找联系人" << str1 << endl;
cout << str1 << "4.删除联系人" << str1 << endl;
cout << str1 << "5.修改联系人" << str1 << endl;
cout << str1 << "6.清除联系人" << str1 << endl;
cout << str1 << "7.退出系统" << str1 << endl;
cout << str2 << endl;
}
3.添加联系人(按名字的拼音排序)
用插入法生成有序链表。
void add(Node*& head)
{
Node* s, * p, * q;
s = new Node;
cin >> s->person.name >> s->person.phone >> s->person.address;
s->next = NULL;
if (head == NULL)//若链表为空,则先建立头结点
{
head = s;
cout << "已成功添加联系人的信息!\n";
return;
}
if (s->person.name < head->person.name)//被插数据最小,插在链表头
{
s->next = head;
head = s;
cout << "已成功添加联系人的信息!\n";
return;
}
for (q = head, p = head->next; p ;q = p, p = p->next)
{//搜索插入,插在链表中间
if (s->person.name < p->person.name)
{
s->next = p;
q->next = s;
cout << "已成功添加联系人的信息!\n";
return;
}
}
q->next = s;//被插数据最大,插在链表尾
cout << "已成功添加联系人的信息!\n";
return;
}
4.显示联系人(并记录总人数)
void show(Node* head)
{
//若列表为空
if (head == NULL)
{
cout << "联系人列表为空!" << endl;
return;
}
//若列表不为空
Node* p = head;
int count = 0;
cout << "姓名" << '\t' << "电话" << '\t' << "住址" << endl;
while (p)
{
cout << p->person.name << '\t' << p->person.phone << '\t' << p->person.address << endl;
count++;
p = p->next;
}
cout << "联系人总人数为:"<< count << endl;
return;
}
5.查找联系人
void search(Node* head)
{
Node* p = head;
string name;
cout << "请输入要查找的联系人的姓名:" << endl;
cin >> name;
while (p)
{
if (p->person.name == name)
{
cout << p->person.name << '\t' << p->person.phone << '\t' << p->person.address << endl;
return;
}
p = p->next;
}
cout << "查无此人" << endl;
return;
}
6.删除联系人
void del(Node* head)
{
Node* p, * q;
string name;
cout << "请输入要删除的联系人的姓名:" << endl;
cin >> name;
if (head == NULL)//若链表为空
{
cout << "联系人列表为空!error" << endl;
return;
}
if (head->person.name == name)//若被删节点是头结点
{
p = head;
head = head->next;
delete p;
p = NULL;
cout << "联系人" << name << "已被删除" << endl;
return;
}
for (q = head, p = head->next; p ;q = p, p = p->next)
{//若被删节点不是头结点,则遍历查找并删除节点
if (p->person.name == name)
{
q->next = p->next;
delete p;
p = NULL;
cout << "联系人" << name << "已被删除" << endl;
return;
}
}
cout << "查无此人!" << endl;//若链表中不存在要删的节点
return;
}
7.修改联系人
void revise(Node* head)
{
Node* p = head;
string name;
cout << "请输入要修改的联系人的姓名:" << endl;
cin >> name;
while (p)
{
if (p->person.name == name)
{
cin >> p->person.name >> p->person.phone >> p->person.address;
return;
}
p = p->next;
}//若联系人列表中存在此人
cout << "查无此人!" << endl;//若联系人列表中不存在此人
return;
}
8.清除联系人
void empty(Node* &head)
{
if (head == NULL)//若链表已为空
{
cout << "链表已为空!\n";
return;
}
Node* p;
while (head)//若链表不为空,则不断删除头结点使其被清空
{
p = head;
head = head->next;
delete p;
p = NULL;
}
cout << "已成功清空链表!\n";
return;
}
三、优化
1.程序暂停和清空控制台
system("pause");//暂停程序的运行,按下任意键后,又可以继续执行
system("cls");//清空控制台界面
目的是让运行结果界面更简洁美观。
2.待补充
四、完整代码
已优化且补充了提示语句。
#include<iostream>
#include<string>
using namespace std;
struct Contacts
{
string name;
string phone;
string address;
};
struct Node
{
Contacts person;
Node* next;
};
void showMenu();
void add(Node*& head);
void show(Node* head);
void search(Node* head);
void del(Node* head);
void revise(Node* head);
void empty(Node* &head);
int main()
{
Node* head = NULL;
while (1)
{
showMenu();//显示菜单
int choice;
cout << "请选择功能序号:\n";
cin >> choice;
switch (choice)
{
case 1://添加联系人
add(head);
break;
case 2://显示联系人
show(head);
break;
case 3://查找联系人
search(head);
break;
case 4://删除联系人
del(head);
break;
case 5://修改联系人
revise(head);
break;
case 6://清空联系人
empty(head);
break;
case 7://退出系统
system("cls");
cout << "欢迎下次使用!\n";
return 0;
}
}
return 0;
}
void showMenu()
{
string str1 = "*****";
string str2 = "**********************";
cout << str2 << endl;
cout << str1 << "1.添加联系人" << str1 << endl;
cout << str1 << "2.显示联系人" << str1 << endl;
cout << str1 << "3.查找联系人" << str1 << endl;
cout << str1 << "4.删除联系人" << str1 << endl;
cout << str1 << "5.修改联系人" << str1 << endl;
cout << str1 << "6.清除联系人" << str1 << endl;
cout << str1 << "7.退出系统**" << str1 << endl;
cout << str2 << endl;
}
void add(Node*& head)
{
Node* s, * p, * q;
s = new Node;
cout << "请输入要添加的联系人的信息:姓名、电话、地址\n";
cin >> s->person.name >> s->person.phone >> s->person.address;
s->next = NULL;
if (head == NULL)
{
head = s;
cout << "已成功添加联系人的信息!\n";
system("pause");
system("cls");
return;
}
if (s->person.name < head->person.name)
{
s->next = head;
head = s;
cout << "已成功添加联系人的信息!\n";
system("pause");
system("cls");
return;
}
for (q = head, p = head->next; p ;q = p, p = p->next)
{
if (s->person.name < p->person.name)
{
s->next = p;
q->next = s;
cout << "已成功添加联系人的信息!\n";
system("pause");
system("cls");
return;
}
}
q->next = s;
cout << "已成功添加联系人的信息!\n";
system("pause");
system("cls");
return;
}
void show(Node* head)
{
//若列表为空
if (head == NULL)
{
cout << "联系人列表为空!" << endl;
system("pause");
system("cls");
return;
}
//若列表不为空
Node* p = head;
int count = 0;
cout << "姓名" << '\t' << "电话" << '\t' << "住址" << endl;
while (p)
{
cout << p->person.name << '\t' << p->person.phone << '\t' << p->person.address << endl;
count++;
p = p->next;
}
cout << "联系人总人数为:"<< count << endl;
system("pause");
system("cls");
return;
}
void search(Node* head)
{
Node* p = head;
string name;
cout << "请输入要查找的联系人的姓名:" << endl;
cin >> name;
while (p)
{
if (p->person.name == name)
{
cout << p->person.name << '\t' << p->person.phone << '\t' << p->person.address << endl;
system("pause");
system("cls");
return;
}
p = p->next;
}
cout << "查无此人" << endl;
system("pause");
system("cls");
return;
}
void del(Node* head)
{
Node* p, * q;
string name;
cout << "请输入要删除的联系人的姓名:" << endl;
cin >> name;
if (head == NULL)
{
cout << "联系人列表为空!error" << endl;
system("pause");
system("cls");
return;
}
if (head->person.name == name)
{
p = head;
head = head->next;
delete p;
p = NULL;
cout << "联系人" << name << "已被删除" << endl;
system("pause");
system("cls");
return;
}
for (q = head, p = head->next; p ;q = p, p = p->next)
{
if (p->person.name == name)
{
q->next = p->next;
delete p;
p = NULL;
cout << "联系人" << name << "已被删除" << endl;
system("pause");
system("cls");
return;
}
}
cout << "查无此人!" << endl;
system("pause");
system("cls");
return;
}
void revise(Node* head)
{
Node* p = head;
string name;
cout << "请输入要修改的联系人的姓名:" << endl;
cin >> name;
while (p)
{
if (p->person.name == name)
{
cout << "请输入要修改的联系人的信息:姓名、电话、地址\n";
cin >> p->person.name >> p->person.phone >> p->person.address;
cout << "已成功修改!\n";
system("pause");
system("cls");
return;
}
p = p->next;
}
cout << "查无此人!" << endl;
system("pause");
system("cls");
return;
}
void empty(Node* &head)
{
if (head == NULL)
{
cout << "链表已为空!\n";
system("pause");
system("cls");
return;
}
Node* p;
while (head)//不断删除头结点
{
p = head;
head = head->next;
delete p;
p = NULL;
}
cout << "已成功清空链表!\n";
system("pause");
system("cls");
return;
}