C++ implementation of linked list version address book
- 2020-06-23 01:15:40
- OfStack
This article example Shared C++ for you to realize the linked list version of the address book specific code, for your reference, specific content is as follows
#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() // Main menu function
{
int choice = 0 ;
cout<<"\t\t\t\t**************************"<<endl;
cout<<"\t\t\t\t* The address book c++ The simple version *"<<endl;
cout<<"\t\t\t\t**************************"<<endl;
cout<<"\t\t\t\t* 1 , add 2 , delete, *"<<endl;
cout<<"\t\t\t\t**************************"<<endl;
cout<<"\t\t\t\t* 3 And check 4 , search, *"<<endl;
cout<<"\t\t\t\t**************************"<<endl;
cout<<"\t\t\t\t* 5 , update, 6 , exit *"<<endl;
cout<<"\t\t\t\t**************************"<<endl;
cout<<"\t\t\t\t Please enter options: ";
cin>>choice;
while(!(choice >= 1&&choice <= 6))
{
while(getchar()!='\n');
cout<<" Input error, please re-input! ";
cin>>choice;
}
return choice;
}
void Address::insert() // Add contacts
{
Contact *p = head;
char relay = 0;
while(p->next != NULL)
{
p = p->next;
}
Contact *person = new Contact;
cout<<" Please enter your name: ";
cin>>person->name;
cout<<" Please enter gender: ";
cin>>person->sex;
cout<<" Please enter the telephone number: ";
cin>>person->tel;
cout<<" Please enter the QQ : ";
cin>>person->QQ;
cout<<" Please enter address: ";
cin>>person->address;
cout<<" Please enter comments: ";
cin>>person->addition;
p->next = person;
person->next = NULL;
cout<<"\n Add successfully. Do you want to continue? ( y/n ) ";
cin>>relay;
while(!(relay == 'y'||relay == 'Y'||relay == 'N'||relay == 'n'))
{
cout<<" Input error, please re-enter ( y/n ) : ";
cin>>relay;
}
if(relay == 'y'||relay == 'y')
{
system("clear");
insert();
}
}
void Address::delete_per() // Delete contacts
{
string m_name;
Contact *p = head;
Contact *pre = head;
int flag = 0;
cout<<" Please enter the name of the contact you want to delete! ";
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<<" Delete successful! "<<endl;
}
else
{
cout<<" The contact you deleted does not exist, delete failed! "<<endl;
}
}
void Address::display() // View contacts
{
Contact *p = head;
while(p->next != NULL)
{
p = p->next;
cout<<endl<<"======================================="<<endl;
cout<<" Name: "<<p->name<<endl;
cout<<" Gender: "<<p->sex<<endl;
cout<<" Telephone: "<<p->tel<<endl;
cout<<"QQ : "<<p->QQ<<endl;
cout<<" Address: "<<p->address<<endl;
cout<<" Remark: "<<p->addition<<endl;
}
}
void Address::search() // Search contacts
{
string m_name;
Contact *p = head;
int flag = 0;
cout<<" Please enter the name of the contact you are searching for :";
cin>>m_name;
while(p->next != NULL)
{
p = p->next;
if(p->name == m_name)
{
cout<<endl<<"======================================="<<endl;
cout<<" Name: "<<p->name<<endl;
cout<<" Gender: "<<p->sex<<endl;
cout<<" Telephone: "<<p->tel<<endl;
cout<<"QQ : "<<p->QQ<<endl;
cout<<" Address: "<<p->address<<endl;
cout<<" Remark: "<<p->addition<<endl;
flag = 1;
}
}
if(flag == 1)
{
cout<<"\n Query successful! "<<endl;
}
else
{
cout<<" The contact you inquired does not exist, delete failed! "<<endl;
}
}
void Address::update() // Modify contact
{
Contact *p = head;
string m_name;
int flag = 0;
cout<<" Please enter the name you want to update: ";
cin>>m_name;
while(p->next != NULL)
{
p = p->next;
if(p->name == m_name)
{
cout<<" Please update gender: ";
cin>>p->sex;
cout<<" Please update your phone number: ";
cin>>p->tel;
cout<<" Please update QQ : ";
cin>>p->QQ;
cout<<" Please update your address: ";
cin>>p->address;
cout<<" Please update comments: ";
cin>>p->addition;
flag = 1;
break;
}
}
if(flag == 1)
{
cout<<"\n The update is successful "<<endl;
}
else
{
cout<<" Check no this person, update failed! "<<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 Press any key to return .....";
getchar();
getchar();
}
return 0;
}