Again don\'t forget please use CLASS (PUBLIC, PRIVATE) in the program please I n
ID: 3727682 • Letter: A
Question
Again don't forget please use CLASS (PUBLIC, PRIVATE) in the program please I need that in the program.
Write a program to build a doubly linked list whose nodes contain First Name - Last Name - Address Phone Number Allow the user to add 5 nodes to the list. The data may be entered in random last name order, but build the list in alphabetical last name sequence. Find the proper insertion location by forward chaining. In other words, start at the beginning of the list. Provide a capability to search for a node based on last name, using backward chaining. In other words, start at the end of the list.Explanation / Answer
Code in c++:
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
//creating class
class list
{
private://data members
string firstName,lastName,address,phoneNo;
list* next;
list* prev;
//methods
public:
void inserts(string first,string last,string add,string phone);
void searchh(string last);
void print();
};
list* first;//create
//print the node data
void list::print() {
cout <<"List :"<<endl;
list *tmp = first;
// No nodes
if ( tmp == NULL ) {
cout << "EMPTY list" << endl;
return;
}//print for all the nodes in the list
while ( tmp != NULL ){
cout<<"First Name : "<<tmp->firstName<<endl;
cout<<"Last Name : "<<tmp->lastName<<endl;
cout<<"Address : "<<tmp->address<<endl;
cout<<"Phone Number : "<<tmp->phoneNo<<endl;
cout <<"--------------------------------"<<endl;
tmp = tmp->next;//next
}
}
//insert the elements in the list
void list::inserts(string first,string last,string add,string phone){
// Create a new list
list* newNode = new list;
newNode->firstName=first;
newNode->lastName=last;
newNode->address=add;
newNode->phoneNo=phone;
newNode->next=NULL;
newNode->prev=NULL;
list *temp=first;
//if nodes is to be inserted at first position
if(temp==NULL || temp->lastName.compare(last)>0)
{
newNode->next=first;
first=newNode;
newNode->prev=NULL;
return;
}//find the node after which we are supposed to insert
while(temp->next!=NULL)
{
int x=temp->lastName.compare(last);
if(x>0) break;//if the first node is smaller than second
temp=temp->next;
}//if the node is last node and its value is small then we are supposed to insert our node at last
if(temp->next==NULL && temp->lastName.compare(last)<0)
{
temp->next=newNode;
newNode->next=NULL;
newNode->prev=temp;
return;
}
//for other cases
temp=temp->prev;
newNode->next=temp->next;
temp->next=newNode;
newNode->prev=temp;
}
//search for last name
void list::searchh(string last){
int k = 0;
list* temp=first;
//if the list is empty
if(temp==NULL)
{
cout<<"List is empty"<<endl;
return;
}
//Last node of the list
temp=this;
//iterate all over the list too reach the last node . to apply backward chaining later
while(temp->next=NULL)
{
temp=temp->next;
}
//backward chaining
while(temp!=first)
{
if(temp->lastName.compare(last)==0)//if found then print and return
{
cout<<"Search successful"<<endl;
temp->print();
return;
}
temp=temp->prev;//reach previous
}
if(temp->lastName.compare(last)==0)//if found at first position after iterating all over the loop
{
cout<<"Search successful"<<endl;
temp->print();
return;
}
cout<<"Not found"<<endl;//not found
}
int main ()
{
first->inserts("ramesh","Gupta","Mumbai","79467793");
first->inserts("titu","sharma","Indore","878723823");
first->inserts("rama","Goel","Agra","87378737");
first->inserts("krishna","shrivastava","Banglore","9882983982");
first->inserts("radhika","shrivastava","Goa","9882922982");
first->print();
first->searchh("Gupta");
}