用C++实现链表版本通讯录

来源:互联网 发布:淘宝网为什么打不开了 编辑:程序博客网 时间:2024/06/10 06:52
#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;}

原创粉丝点击