项目参考来源:【C++简单项目】用单链表制作通讯录_数据结构用链表做通讯录-CSDN博客
项目需要实现的功能:
1.添加联系人
2.显示联系人
3.查找联系人
4.删除联系人
5.修改联系人
6.清除联系人
7.退出联系人
各个功能函数命名
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);
确定我们需要用到的库函数
#include<iostream>
#include<string>
调用命名空间,简化使用std命名空间里的函数使用
关于namespace:为什么在C++程序中写“using namespace std”很重要?|极客笔记 (deepinout.com)
using namespace std;
定义我们需要的数据结构
一个是联系人,一个是链表
struct Contacts
{
string name;
string phone;
string address;
};
struct Node
{
Contacts person;
Node *next;
};
主函数
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://退出联系人
cout << "欢迎下次使用" << endl;
return 0;
default:
break;
}
}
}
1.添加联系人
这是一个插入链表的操作,插入的名字按c++默认的string排序,1在2前,a在b前
void add(Node *&head)
{
Node *s,*p,*q ;
s = new Node;
cout << "请输入名字:\n";
cin >> s->person.name;
cout << "请输入电话:\n";
cin >> s->person.phone;
cout << "请输入住址:\n";
cin >> 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;
}
2.显示联系人
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;
}
3.查找联系人
查找链表中的元素
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;
}
4.删除联系人
单链表中存在一个删除的问题,当链表的最后一个结点需要被删除时,无法操作它的后继结点和前驱节点,因此笔者想到了另一个简单的if语句,使得前驱结点的位置能够保留,在笔者的另一篇博客中分析了删除操作和时间复杂度的问题
void del(Node *head)
{
Node *p = head;
string name;
cout << "请输入要删除的联系人的姓名:" << endl;
cin >> name;
if (head == NULL)
{
cout << "联系人列表为空!" << endl;
return;
}
while(p)
{
if (p->next->person.name == name&&p->next->next==NULL)
{
Node *q = p->next;
p->next = q->next;
free(q);
cout << "已删除,此时为最后一个结点" << endl;
return;
}
if (p->person.name == name)
{
Node* q = p->next;
//Node *s=(Node *)malloc(sizeof(Node));
p->person.name = p->next->person.name;
p->person.phone = p->next->person.phone;
p->person.address = p->next->person.address;
p->next = q->next;
free(q);
cout << "已删除" << endl;
return;
}
p=p->next;
}
cout << "查无此人" << endl;
}
5.修改联系人
在修改联系人的功能函数中加入了检测输入是否符合规定变量的类型,如果说定义了一个int变量,输入string变量就很尴尬。C++为我们提供了cin.fail(),cin.bad()函数来判断检测输入是否成功。
检查输入函数以及修改联系人函数
int judge(int temp)
{
//对输入的合法性进行判断
cin.sync();//清空输入流缓冲区
cin >> temp;
while (1)
{
if (cin.fail() || cin.bad() || cin.get() != '\n')
cout << "输入需要对应,请重新输入:" << endl;
else break;
cin.clear();
cin.sync();
cin >> temp;
}
return temp;
}
void revise(Node* head)
{
Node *p = head;
string name;
if (head == NULL)
{
cout << "列表为空,没有可修改的联系人" << endl;
return;
}
cout << "请输入要修改的联系人姓名:" << endl;
cin >> name;
while(p)
{
if (p->person.name == name)
{
cout << "请输入需要修改的部分" << endl;
cout << "1 姓名" << '\t' << "2 电话" << '\t' << "3 住址" << '\t' << "4 全部" << endl;
int choice;
string rename,rephone,readdress;
choice = judge(choice);
switch (choice)
{
case 1:
cout << "请输入重新修改的名字:" << endl;
cin >> rename;
p->person.name = rename;
cout << "修改成功!\n";
return;
case 2:
cout << "请输入重新修改的电话:" << endl;
cin >> rephone;
p->person.phone = rephone;
cout << "修改成功!\n";
return;
case 3:
cout << "请输入重新修改的住址:" << endl;
cin >> readdress;
p->person.address = readdress;
cout << "修改成功!\n";
return;
case 4:
cout << "请输入名字:\n";
cin >> p->person.name;
cout << "请输入电话:\n";
cin >> p->person.phone;
cout << "请输入住址:\n";
cin >> p->person.address;
cout << "修改成功!\n";
return;
default:
return;
}
}
p = p->next;
}
cout << "没有找到该联系人" << endl;
}
6.清除联系人
用到了上述提到的检测函数,可以在确认清除操作前再确认,以防数据误操作造成丢失
void empty(Node *&head)
{
if (head == NULL)
{
cout << "链表已为空!" << endl;
return;
}
int confirm;
cout << "确认清空联系人吗?这可能会导致联系人丢失" << '\n' << "请输入任意数字确认清空操作" << endl;
judge(confirm);
Node *p;
while (head)
{
p = head;
head = head->next;
free(p);
}
cout << "联系人列表已清空!" << endl;
return;
}
完整代码
#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 judge(int temp)
{
//对输入的合法性进行判断
cin.sync();//清空输入流缓冲区
cin >> temp;
while (1)
{
if (cin.fail() || cin.bad() || cin.get() != '\n')
cout << "输入需要对应,请重新输入:" << endl;
else break;
cin.clear();
cin.sync();
cin >> temp;
}
return temp;
}
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://退出联系人
cout << "欢迎下次使用" << endl;
return 0;
default:
break;
}
}
}
void showMenu()
{
string str1 = "————————————————";
string str2 = "*************";
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;
cout << "请输入电话:\n";
cin >> s->person.phone;
cout << "请输入住址:\n";
cin >> 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;
}
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;
}
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;
}
void del(Node *head)
{
Node *p = head;
string name;
cout << "请输入要删除的联系人的姓名:" << endl;
cin >> name;
if (head == NULL)
{
cout << "联系人列表为空!" << endl;
return;
}
while(p)
{
if (p->next->person.name == name&&p->next->next==NULL)
{
Node *q = p->next;
p->next = q->next;
free(q);
cout << "已删除,此时为最后一个结点" << endl;
return;
}
if (p->person.name == name)
{
Node* q = p->next;
//Node *s=(Node *)malloc(sizeof(Node));
p->person.name = p->next->person.name;
p->person.phone = p->next->person.phone;
p->person.address = p->next->person.address;
p->next = q->next;
free(q);
cout << "已删除" << endl;
return;
}
p=p->next;
}
cout << "查无此人" << endl;
}
void revise(Node* head)
{
Node *p = head;
string name;
if (head == NULL)
{
cout << "列表为空,没有可修改的联系人" << endl;
return;
}
cout << "请输入要修改的联系人姓名:" << endl;
cin >> name;
while(p)
{
if (p->person.name == name)
{
cout << "请输入需要修改的部分" << endl;
cout << "1 姓名" << '\t' << "2 电话" << '\t' << "3 住址" << '\t' << "4 全部" << endl;
int choice;
string rename,rephone,readdress;
choice = judge(choice);
switch (choice)
{
case 1:
cout << "请输入重新修改的名字:" << endl;
cin >> rename;
p->person.name = rename;
cout << "修改成功!\n";
return;
case 2:
cout << "请输入重新修改的电话:" << endl;
cin >> rephone;
p->person.phone = rephone;
cout << "修改成功!\n";
return;
case 3:
cout << "请输入重新修改的住址:" << endl;
cin >> readdress;
p->person.address = readdress;
cout << "修改成功!\n";
return;
case 4:
cout << "请输入名字:\n";
cin >> p->person.name;
cout << "请输入电话:\n";
cin >> p->person.phone;
cout << "请输入住址:\n";
cin >> p->person.address;
cout << "修改成功!\n";
return;
default:
return;
}
}
p = p->next;
}
cout << "没有找到该联系人" << endl;
}
void empty(Node *&head)
{
if (head == NULL)
{
cout << "链表已为空!" << endl;
return;
}
int confirm;
cout << "确认清空联系人吗?这可能会导致联系人丢失" << '\n' << "请输入任意数字确认清空操作" << endl;
judge(confirm);
Node *p;
while (head)
{
p = head;
head = head->next;
free(p);
}
cout << "联系人列表已清空!" << endl;
return;
}
如果对你有帮助,请用你的赞和收藏淹没我吧~