Answers
#include<iostream>
#include<string>
#include<stdlib.h>
using namespace std;
// Class List definition
class List
{
public:
// Data member to store data
string name;
// Pointer to point to next node
List *next;
};// End of class
// Declares a start pointer and assigns null to it
List *start = NULL;
// Function to add a name
void addName()
{
string name;
// Creates a temporary pointer
List *temp = new List();
// Checks if temp is null then list is full
if(temp==NULL)
{
cout<<"\nOut of Memory Space:\n";
return;
}// End of if condition
cout<<"\n Enter a name to add: ";
cin>>temp->name;
// Assigns null to temp next
temp->next = NULL;
// Checks if the start is NULL then it is the first node to insert
if(start == NULL)
{
// temp address is assigned to start
start = temp;
}// End of if condition
// Otherwise
else
{
// start address is assigned to temp next
temp->next = start;
// temp address is assigned to start
start = temp;
}// End of else
}// End of function
// Function to display information
void displayNames()
{
// Creates a temporary pointer
List *ptr;
// Checks if start is null then list is empty
if(start == NULL)
{
cout<<"\nList is empty:\n";
return;
}// End of if condition
else
{
// Points to starting name
ptr = start;
cout<<"\n List of names \n";
// Loops till end of the list
while(ptr != NULL)
{
cout<<"\n Name: "<<ptr->name;
// Move to next name
ptr = ptr->next ;
}// End of while loop
}// End of else
}// End of function
// Function to search a name in the list
int searchName(string name)
{
int position = -1, counter = 0;
// Creates a temporary pointers
List *ptr;
// Checks if start is null then list is empty
if(start==NULL)
{
cout<<"\nThe List is Empty:\n";
return -1;
}// End of if condition
// Otherwise
else
{
// Points to starting name
ptr = start;
// Loops till end of the list
while(ptr != NULL)
{
// Checks if current name number is equals to parameter name
if(ptr->name.compare(name) == 0)
{
cout<<"\n Name "<<name<<" available.";
// Assigns the found position
position = counter;
// Returns the position
return counter;
}// End of if condition
// Move to next name
ptr=ptr->next ;
// Increase the record counter
counter++;
}// End of while loop
}// End of else
// Checks if found position is -1 name not found
if(position == -1)
cout<<"\n Name "<<name<<" not available.";
// returns the position
return position;
}// End of function
// Function to delete a node at specified position
void deleteName()
{
int position;
string name;
// Creates a temporary pointer
List *temp,*ptr;
// Checks if start is null then list is empty
if(start==NULL)
{
cout<<"\nThe List is Empty:\n";
return;
}// End of if condition
// Otherwise
else
{
// Accepts the name
cout<<"\nEnter the name: \t";
cin>>name;
// Calls the function to search the name
// Stores the returned found position
position = searchName(name);
// Checks if found position is -1 name not found
if(position == -1)
{
cout<<"\n No such "<<name<<" found to delete.";
return;
}// End of if condition
// Checks if the position is zero
if(position == 0)
{
// Temporary pointer points start
ptr = start;
// Start pointing to next node
start = start->next;
cout<<"\n "<<name<<" deleted from list.";
// Deletes the pointer
free(ptr);
}// End of if condition
// Otherwise
else
{
// Temporary pointer points start
ptr = start;
// Loops till record found position
for(int c = 0; c < position; c++)
{
// temporary pointer pointing to previous node ptr
temp = ptr;
// Move to next name
ptr = ptr->next;
// Checks if ptr is null node not found
if(ptr == NULL)
{
cout<<"\n Name: "<<name<<" not found in the list.\n";
return;
}// End of if condition
}// End of for loop
temp->next = ptr->next ;
cout<<"\n The deleted name: "<<name;
// Deletes the name node
free(ptr);
}// End of inner else
}// End of outer else
}// End of function
// Function to display user choice and return user choice
int menu()
{
// To store user choice
int choice;
// Displays menu
cout<<"\n LIST MENU \n";
cout<<"---------------------------------------\n";
cout<<"\n 1. Add Name.";
cout<<"\n 2. Delete name.";
cout<<"\n 3. Search name.";
cout<<"\n 4. Display all names.";
cout<<"\n 5.
Quit";
cout<<"\n--------------------------------------\n";
// Accepts user choice
cout<<"Enter your choice:\t";
cin>>choice;
// returns user choice
return choice;
}// End of function
// main function definition
int main()
{
string name;
// Loops till user choice is not 5
while(1)
{
// Calls the function to display menu.
// Calls the function as per user choice returned by the function
switch(menu())
{
case 1:
addName();
break;
case 2:
deleteName();
break;
case 3:
cout<<"\n Enter a name to search: ";
cin>>name;
searchName(name);
break;
case 4:
displayNames();
break;
case 5:
exit(0);
default:
cout<<"\n Wrong Choice:\n";
}//end of switch - case
}// End of while loop
return 0;
}//end of main function
Sample Output:
LIST MENU
---------------------------------------
1. Add Name.
2. Delete name.
3. Search name.
4. Display all names.
5.
Quit
--------------------------------------
Enter your choice: 4
List is empty:
LIST MENU
---------------------------------------
1. Add Name.
2. Delete name.
3. Search name.
4. Display all names.
5.
Quit
--------------------------------------
Enter your choice: 3
Enter a name to search: Mohan
The List is Empty:
LIST MENU
---------------------------------------
1. Add Name.
2. Delete name.
3. Search name.
4. Display all names.
5.
Quit
--------------------------------------
Enter your choice: 2
The List is Empty:
LIST MENU
---------------------------------------
1. Add Name.
2. Delete name.
3. Search name.
4. Display all names.
5.
Quit
--------------------------------------
Enter your choice: 1
Enter a name to add: Pyari
LIST MENU
---------------------------------------
1. Add Name.
2. Delete name.
3. Search name.
4. Display all names.
5.
Quit
--------------------------------------
Enter your choice: 1
Enter a name to add: Mohan
LIST MENU
---------------------------------------
1. Add Name.
2. Delete name.
3. Search name.
4. Display all names.
5.
Quit
--------------------------------------
Enter your choice: 1
Enter a name to add: Sahu
LIST MENU
---------------------------------------
1. Add Name.
2. Delete name.
3. Search name.
4. Display all names.
5.
Quit
--------------------------------------
Enter your choice: 1
Enter a name to add: Ram
LIST MENU
---------------------------------------
1. Add Name.
2. Delete name.
3. Search name.
4. Display all names.
5.
Quit
--------------------------------------
Enter your choice: 2
Enter the name: sunita
Name sunita not available.
No such sunita found to delete.
LIST MENU
---------------------------------------
1. Add Name.
2. Delete name.
3. Search name.
4. Display all names.
5.
Quit
--------------------------------------
Enter your choice: 2
Enter the name: Ram
Name Ram available.
Ram deleted from list.
LIST MENU
---------------------------------------
1. Add Name.
2. Delete name.
3. Search name.
4. Display all names.
5.
Quit
--------------------------------------
Enter your choice: 4
List of names
Name: Sahu
Name: Mohan
Name: Pyari
LIST MENU
---------------------------------------
1. Add Name.
2. Delete name.
3. Search name.
4. Display all names.
5.
Quit
--------------------------------------
Enter your choice: 3
Enter a name to search: Sita
Name Sita not available.
LIST MENU
---------------------------------------
1. Add Name.
2. Delete name.
3. Search name.
4. Display all names.
5.
Quit
--------------------------------------
Enter your choice: 3
Enter a name to search: Pyari
Name Pyari available.
LIST MENU
---------------------------------------
1. Add Name.
2. Delete name.
3. Search name.
4. Display all names.
5.
Quit
--------------------------------------
Enter your choice: 9
Wrong Choice:
LIST MENU
---------------------------------------
1. Add Name.
2. Delete name.
3. Search name.
4. Display all names.
5.
Quit
--------------------------------------
Enter your choice: 5