In C++, this is taking the array list and turning it into a linked list. Using c
ID: 3914112 • Letter: I
Question
In C++, this is taking the array list and turning it into a linked list.
Using classes and linked lists, design an Online Address Book to keep track of the names, addresses,phone numbers, and dates of birth of family members, close friends, and certain business associates.Your program should be able to handle as many entries as required.
1. By the Assignment 2, you have created the user defined data type extPersonType
2. Define the class addressBookType to built a linked list. The value of each node has the type of extPersonType . An object of the type addressBookType
should be able to process as many entries each of them as required.
3. Write a C++ program to test your design. The program should perform the following operations:
Add a new entry to a address book.
Delete an entry from a address book.
Print the value of each node of a address book.
Explanation / Answer
Below is the C++ code I hope that i have provided sufficient comments for your better understanding Note that I have done proper indentation but this code is automatically left alligned on this interface
#include<bits/stdc++.h>
using namespace std;
struct extPerson
{
string name,address,phone,dob;
};
class addressBook
{
private:
extPerson data;
addressBook *next;
public:
void setValue(string n,string a,string p,string d)
{
data.name=n;
data.address=a;
data.phone=p;
data.dob=d;
next=NULL;
}
//add an entry
void add(addressBook obj)
{
addressBook *temp=this;
//insert this entry at end
while(temp->next!=NULL)
temp=temp->next;
temp->next=&obj;
}
void remov()
{
addressBook *temp=this;
addressBook *prev=NULL;
while(temp->next!=NULL)
{
prev=temp;
temp=temp->next;
}
delete prev->next;
prev->next=NULL;
}
void display()
{
addressBook *temp=this;
//No entry is present
if(temp==NULL)
cout<<"List is empty"<<endl;
else
{
int i=1;
while(temp!=NULL)
{
cout<<"Entry "<<i<<endl;
cout<<"Name is : "<<temp->data.name<<endl;
cout<<"Address is : "<<temp->data.address<<endl;
cout<<"Phone is : "<<temp->data.phone<<endl;
cout<<"Date of birth is : "<<temp->data.dob<<endl;
temp=temp->next;
i++;
}
}
}
};
int main()
{
int choice;
cout<<"Select your choice ";
cout<<"1. Add entry ";
cout<<"2. Delete entry ";
cout<<"3. Print entry ";
cout<<"4. Exit ";
string name,add,dob,phone;
addressBook *head=NULL,obj;
while(1)
{
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter name : ";
cin>>name;
cout<<"Enter address : ";
cin>>add;
cout<<"Enter phone number : ";
cin>>phone;
cout<<"Enter date of birth : ";
cin>>dob;
obj.setValue(name,add,phone,dob);
if(head==NULL)
head=&obj;
else
head->add(obj);
break;
case 2:
if(head==NULL)
cout<<"List is already empty"<<endl;
else
head->remov();
break;
case 3:
head->display();
break;
case 4:
exit(0);
}
}
return 0;
}
Hope i have answered your question satisfactorily.Leave doubts in comment section if any.