C++怎么实现链表版本通讯录

发布时间:2021-04-14 11:31:05 作者:小新
来源:亿速云 阅读:216

这篇文章将为大家详细讲解有关C++怎么实现链表版本通讯录,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

具体内容如下

#include <iostream>
#include <string>
using namespace std;
class Address;
 
class Contact{
private:
 string name;
 string sex;
 string tel;
 string QQ;
 string address;
 string addition;
 Contact *next;
public:
 Contact();
 friend class Address; 
 
};
Contact::Contact()
{
 next = NULL;
}
class Address{
public:
 Address();
 ~Address();
 int show();
 void insert();
 void delete_per();
 void display();
 void search();
 void update(); 
private:
 Contact *head;
};
Address::Address()
{
 head = new Contact;
 if(head == NULL)
 {
 cout<<"fail create"<<endl;
 }
}
Address::~Address()
{
 delete head;
}
int Address::show()  //主菜单函数
{
 int choice = 0 ;
 cout<<"\t\t\t\t**************************"<<endl;
 cout<<"\t\t\t\t* 通讯录c++简易版本 *"<<endl;
 cout<<"\t\t\t\t**************************"<<endl;
 cout<<"\t\t\t\t* 1、添加 2、删除  *"<<endl;
 cout<<"\t\t\t\t**************************"<<endl;
 cout<<"\t\t\t\t* 3、查看 4、搜索  *"<<endl;
 cout<<"\t\t\t\t**************************"<<endl;
 cout<<"\t\t\t\t* 5、更新 6、退出  *"<<endl;
 cout<<"\t\t\t\t**************************"<<endl;
 cout<<"\t\t\t\t请输入选择:";
 cin>>choice;
 while(!(choice >= 1&&choice <= 6))
 {
 while(getchar()!='\n');
 cout<<"输入有误,请重新输入!";
 cin>>choice;
 }
 return choice;
 
}
void Address::insert() //添加联系人
{
 Contact *p = head;
 char relay = 0;
 while(p->next != NULL)
 {
 p = p->next;
 }
 Contact *person = new Contact;
 cout<<"请输入姓名:";
 cin>>person->name;
 cout<<"请输入性别:";
 cin>>person->sex;
 cout<<"请输入电话:";
 cin>>person->tel;
 cout<<"请输入QQ:";
 cin>>person->QQ;
 cout<<"请输入住址:";
 cin>>person->address;
 cout<<"请输入备注:";
 cin>>person->addition;
 p->next = person;
 person->next = NULL;
 cout<<"\n添加成功,是否继续添加?(y/n)";
 cin>>relay;
 while(!(relay == 'y'||relay == 'Y'||relay == 'N'||relay == 'n'))
 {
 cout<<"输入错误,请重新输入(y/n):";
 cin>>relay;
 }
 if(relay == 'y'||relay == 'y')
 {
 system("clear");
 insert();
 }
}
void Address::delete_per() //删除联系人
{
 string m_name;
 Contact *p = head;
 Contact *pre = head;
 int flag = 0;
 cout<<"请输入你要删除的联系人姓名!";
 cin>>m_name;
 while(p->next != NULL)
 {
 pre = p;
 p = p->next;
 if(p->name == m_name)
 {
 pre->next = p->next;
 delete p;
 p = NULL;
 flag = 1;
 break;
 }
 }
 if(flag == 1)
 {
 cout<<"删除成功!"<<endl;
 }
 else
 {
 cout<<"您删除的联系人不存在,删除失败!"<<endl;
 }
}
 
void Address::display() //查看联系人 
{
 Contact *p = head;
 while(p->next != NULL)
 {
 p = p->next;
 cout<<endl<<"======================================="<<endl;
 cout<<"姓名:"<<p->name<<endl;
 cout<<"性别:"<<p->sex<<endl;
 cout<<"电话:"<<p->tel<<endl;
 cout<<"QQ:"<<p->QQ<<endl;
 cout<<"地址:"<<p->address<<endl;
 cout<<"备注:"<<p->addition<<endl; 
 }
 
}
void Address::search() //搜索联系人
{
 string m_name;
 Contact *p = head;
 int flag = 0;
 cout<<"请输入你要搜索的联系人姓名:";
 cin>>m_name;
 while(p->next != NULL)
 {
 p = p->next;
 if(p->name == m_name)
 {
 cout<<endl<<"======================================="<<endl;
 cout<<"姓名:"<<p->name<<endl;
 cout<<"性别:"<<p->sex<<endl;
 cout<<"电话:"<<p->tel<<endl;
 cout<<"QQ:"<<p->QQ<<endl;
 cout<<"地址:"<<p->address<<endl;
 cout<<"备注:"<<p->addition<<endl; 
 flag = 1;
 }
 }
 if(flag == 1)
 {
 cout<<"\n查询成功!"<<endl;
 }
 else
 {
 cout<<"您查询的联系人不存在,删除失败!"<<endl;
 }
}
void Address::update() //修改联系人
{
 Contact *p = head;
 string m_name;
 int flag = 0;
 
 cout<<"请输入你要更新的姓名:";
 cin>>m_name;
 while(p->next != NULL)
 {
 p = p->next;
 if(p->name == m_name)
 {
 cout<<"请更新性别:";
 cin>>p->sex;
 cout<<"请更新电话:";
 cin>>p->tel;
 cout<<"请更新QQ:";
 cin>>p->QQ;
 cout<<"请更新住址:";
 cin>>p->address;
 cout<<"请更新备注:";
 cin>>p->addition;
 flag = 1;
 break;
 }
 }
 if(flag == 1)
 {
 cout<<"\n更新成功"<<endl;
 }
 else
 {
 cout<<"查无此人,更新失败!"<<endl;
 }
}
 
int main()
{
 Address *person = new Address;
 int choice = 0;
 while(1)
 {
 system("clear");
 choice = person->show();
 switch(choice)
 {
 case 1:
 {
 system("clear");
 person->insert();
 break;
 }
 case 2:
 {
 system("clear");
 person->delete_per();
 break;
 }
 case 3:
 {
 system("clear");
 person->display();
 break;
 }
 case 4:
 {
 system("clear");
 person->search();
 break;
 }
 case 5:
 {
 system("clear");
 person->update();
 break;
 }
 case 6:
 {
 exit(0);
 }
 }
 cout<<"\n\n按任意键返回.....";
 getchar();
 getchar();
 }
 return 0;
}

关于“C++怎么实现链表版本通讯录”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

推荐阅读:
  1. C++双向链表如何实现简单通讯录
  2. 怎么在C++中利用链表实现通讯录管理系统

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

c++

上一篇:怎么使用opencv3/C++实现霍夫圆/直线检测

下一篇:C++怎么实现俄罗斯方块

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》