Implement a Linked List program that contains the following functionality: • ADD
ID: 664194 • Letter: I
Question
Implement a Linked List program that contains the following functionality:
• ADD a "name" (a simple character string of no more than 30 characters), X, and store the "name" alphabetically in the Linked List.
o Assign a unique identifier to each "name" (how to handle duplicates?)
o If the "name" is a duplicate of one already in the Address Book, print to the screen: "DUPLICATE: X" • SEARCH for a "name", X, in the Address Book.
o If found, print to the screen “TRUE: X”.
o If the "name" does not exist, print to the screen “FALSE: X”.
• REMOVE a "name", X, from the Address Book.
o If found, print to the screen “Removal successful: X ”
o If the "name" does not exist, print to the screen “ERROR: Not found for Removal: X”.
PRINT the "names" in the alphabetical order
FILE -- process commands from the filename provided (prompt for the filename)
QUIT -- close all files and end program.
PROGRAM SPECIFICATION:
When the program starts, the user should be prompted with a welcome screen and a list of options (add, remove, print tree, and QUIT). Such as:
Pat's ADDRESS BOOK cmd>
Explanation / Answer
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
// Declaration of the Node
struct node
{
int info;
struct node *next;
}*last;
// Declaration of the Class
class c_llist
{
public:
void c_node(int value);
void a_begin(int value);
void a_after(int value, int position);
void d_element(int value);
void s_element(int value);
void d_list();
void sort();
c_llist()
{
last = NULL;
}
};
// main method
int main()
{
int ch, elmt, position;
c_llist cl;
while (1)
{
cout<<endl<<"---------------------------"<<endl;
cout<<endl<<"Linked list"<<endl;
cout<<endl<<"---------------------------"<<endl;
cout<<"1.Create Node"<<endl;
cout<<"2.Add at beginning"<<endl;
cout<<"3.Add middle"<<endl;
cout<<"4.Delete"<<endl;
cout<<"5.Search"<<endl;
cout<<"6.Display"<<endl;
cout<<"7.Sort"<<endl;
cout<<"8.Quit"<<endl;
cout<<"Enter your choice : ";
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter the element: ";
cin>>elmt;
cl.c_node(elmt);
cout<<endl;
break;
case 2:
cout<<"Enter the element: ";
cin>>elmt;
cl.a_begin(elmt);
cout<<endl;
break;
case 3:
cout<<"Enter the element: ";
cin>>elmt;
cout<<"Insert element position: ";
cin>>position;
cl.a_after(elmt, position);
cout<<endl;
break;
case 4:
if (last == NULL)
{
cout<<"List is empty, nothing is available to delete"<<endl;
break;
}
cout<<"Enter the element to delete: ";
cin>>elmt;
cl.d_element(elmt);
cout<<endl;
break;
case 5:
if (last == NULL)
{
cout<<"Can't search in empty list"<<endl;
break;
}
cout<<"Enter the element to be search: ";
cin>>elmt;
cl.s_element(elmt);
cout<<endl;
break;
case 6:
cl.d_list();
break;
case 7:
cl.sort();
break;
case 8:
exit(1);
break;
default:
cout<<"Wrong choice"<<endl;
}
}
return 0;
}
// Linked list creation
void c_llist::c_node(int value)
{
struct node *temp;
temp = new(struct node);
temp->info = value;
if (last == NULL)
{
last = temp;
temp->next = last;
}
else
{
temp->next = last->next;
last->next = temp;
last = temp;
}
}
// Method to insert the element
void c_llist::a_begin(int value)
{
if (last == NULL)
{
cout<<"First Create the list."<<endl;
return;
}
struct node *temp;
temp = new(struct node);
temp->info = value;
temp->next = last->next;
last->next = temp;
}
// Method to insert element at a particular place
void c_llist::a_after(int value, int pos)
{
if (last == NULL)
{
cout<<"First Create the list."<<endl;
return;
}
struct node *temp, *s;
s = last->next;
for (int i = 0;i < pos-1;i++)
{
s = s->next;
if (s == last->next)
{
cout<<"There are less than ";
cout<<pos<<" in the list"<<endl;
return;
}
}
temp = new(struct node);
temp->next = s->next;
temp->info = value;
s->next = temp;
if (s == last)
{
last=temp;
}
}
// Method to Delete a element from the list
void c_llist::d_element(int value)
{
struct node *temp, *s;
s = last->next;
if (last->next == last && last->info == value)
{
temp = last;
last = NULL;
free(temp);
return;
}
if (s->info == value)
{
temp = s;
last->next = s->next;
free(temp);
return;
}
while (s->next != last)
{
if (s->next->info == value)
{
temp = s->next;
s->next = temp->next;
free(temp);
cout<<"Element "<<value;
cout<<" deleted from the list"<<endl;
return;
}
s = s->next;
}
if (s->next->info == value)
{
temp = s->next;
s->next = last->next;
free(temp);
last = s;
return;
}
cout<<"Element "<<value<<" not found in the list"<<endl;
}
// Method to search element in the list
void c_llist::s_element(int value)
{
struct node *s;
int counter = 0;
s = last->next;
while (s != last)
{
counter++;
if (s->info == value)
{
cout<<"Element "<<value;
cout<<" found at position "<<counter<<endl;
return;
}
s = s->next;
}
if (s->info == value)
{
counter++;
cout<<"Element "<<value;
cout<<" found at position "<<counter<<endl;
return;
}
cout<<"Element "<<value<<" not found in the list"<<endl;
}
///Method to display Link List
void c_llist::d_list()
{
struct node *s;
if (last == NULL)
{
cout<<"List is empty, nothing to display"<<endl;
return;
}
s = last->next;
cout<<"Link List: "<<endl;
while (s != last)
{
cout<<s->info<<"->";
s = s->next;
}
cout<<s->info<<endl;
}
// Method to sort Link List
void c_llist::sort()
{
struct node *s, *ptr;
int temp;
if (last == NULL)
{
cout<<"List is empty, nothing to sort"<<endl;
return;
}
s = last->next;
while (s != last)
{
ptr = s->next;
while (ptr != last->next)
{
if (ptr != last->next)
{
if (s->info > ptr->info)
{
temp = s->info;
s->info = ptr->info;
ptr->info = temp;
}
}
else
{
break;
}
ptr = ptr->next;
}
s = s->next;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.