In: Computer Science
Create an ADT class that creates a friend contact list. The program should be able to add, remove and view the contacts. In C++
#include<iostream>
#include<cstdlib>
#include<string.h>
using namespace std;
//contact class
class contact
{
public:
//data members
char name[30];//name
char phone[30];//phone number
//add any details you want
//pointer to next contact
contact *next;
//constructor
contact()
{
cout<<"Enter name :";
string n;
getline(cin,n);//reading name
int i=0;
for(i=0;n[i]!='\0';i++)name[i]=n[i];
name[i]='\0';
cout<<"Enter phone number:
";
string p;
getline(cin,p);
for(i=0;p[i]!='\0';i++)phone[i]=p[i];
phone[i]='\0';
next=NULL;
}
};
//friend contact list class
class contact_list
{
contact *list;
public:
//contstructor
contact_list()
{
list=NULL;
}
//method to add contact
void Add()
{
contact *n = new contact();
if(list==NULL)list=n;
else
{
n->next =
list;
list =
n;
}
}
//method to remove contact
void Remove()
{
string nam;
//reading name to remove
cout<<"Enter name to remove
:";
getline(cin,nam);
char n[30];
for(int
i=0;nam[i]!='\0';i++)n[i]=nam[i];
contact *temp = list;
contact *prev= NULL;
while(temp!=NULL)
{
if(strcmp(n,temp->name)==0)//means name is found
break;
prev=temp;
temp=temp->next;
}
if(temp==NULL)
{
cout<<"Name not found in contact list\n";
}
else
{
if(prev==NULL)
list=list->next;
else
{
prev->next=temp->next;
}
}
}
//method to display all contacts
void Display()
{
contact *temp = list;
while(temp!=NULL)
{
cout<<"Name :"<<temp->name<<endl;
cout<<"Phone number
:"<<temp->phone<<endl;
cout<<endl;
temp=temp->next;
}
}
};
int main()
{
contact_list *list= new contact_list();;
//testing
int c;
while(1)
{
cout<<"1:Add
contact\n2:Remove contact\n3:View contacts\n4:Exit\nEnter :";
cin>>c;
if(c==1)
{string cc;
getline(cin,cc);//reading empty
line
list->Add();
}
else if(c==2)
{string cc;
getline(cin,cc);//reading empty
line
list->Remove();
}
else if(c==3)
{
string cc;
getline(cin,cc);
list->Display();
}
else if(c==4)
break;
}
return 0;
}
output:
1:Add contact
2:Remove contact
3:View contacts
4:Exit
Enter :1
Enter name :Surya
Enter phone number: 9887654322
1:Add contact
2:Remove contact
3:View contacts
4:Exit
Enter :3
Name :Surya
Phone number :9887654322
1:Add contact
2:Remove contact
3:View contacts
4:Exit
Enter :1
Enter name :phane
Enter phone number: 23456789
1:Add contact
2:Remove contact
3:View contacts
4:Exit
Enter :3
Name :phane
Phone number :23456789
Name :Surya
Phone number :9887654322
1:Add contact
2:Remove contact
3:View contacts
4:Exit
Enter :2
Enter name to remove :phane
1:Add contact
2:Remove contact
3:View contacts
4:Exit
Enter :3
Name :Surya
Phone number :9887654322
1:Add contact
2:Remove contact
3:View contacts
4:Exit
Enter :2
Enter name to remove :asdf
Name not found in contact list
1:Add contact
2:Remove contact
3:View contacts
4:Exit
Enter :3
Name :Surya
Phone number :9887654322
1:Add contact
2:Remove contact
3:View contacts
4:Exit
Enter :1
Enter name :Hello
Enter phone number: 123456789
1:Add contact
2:Remove contact
3:View contacts
4:Exit
Enter :3
Name :Hello
Phone number :123456789
Name :Surya
Phone number :9887654322
1:Add contact
2:Remove contact
3:View contacts
4:Exit
Enter :4
Process exited normally.
Press any key to continue . . .
//PLS give a thumbs up if you find this helpful, it helps me alot,
thanks.