Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a class that implements an ordered linked list based on the following UML

ID: 3668362 • Letter: W

Question

Write a class that implements an ordered linked list based on the following UML diagram:

The { query } tag indicates the method is an accessor.

The class contains a single attribute, head. Head is a pointer to the first node in the linked list.

The class contains several required methods and one extra credit method.

The required methods are:

The extra credit methods are:


The extra credit is worth 1 point.

Demonstrate your class in a menu-driven program.

The program should prompt the user for a selection from the following choices:

(C)lear
when the user selects this option, the linked list is destroyed.

(D)elete
Selecting this option prompts the user for a character to remove from the linked list, and then attempts to remove it.

(G)et
Loads the linked list from disk

(I)nsert
Prompts the user for a character to insert into the linked list, then the insertion is performed.

(L)ength
Returns a count of the number of nodes currently in the linked list

(M)enu
Displays a menu of options

(P)rint
Displays the contents of the linked list

(Q)uit
Terminates the program.

(R)everse
Extra credit option: displays the linked list in reverse order.

(S)earch
Asks the user for a value to search for. Indicates whether the value has been found or not.

(W)rite;
Writes the linked list to disk.

The program should continue prompting the user for selections and processing them until the user select Quit. Use your object methods to process each request.

Explanation / Answer

Code:

#include <iostream>
#include <fstream>
using namespace std;
struct node
{
char value;
struct node *next;
};
class linked_list
{
private:
node *head;
public:
linked_list()
{
head = new node;
head->next = NULL;
head->value='0';
}
~linked_list()
{
cout<<"the linked list is destroyed"<<endl;
}
void Insert(char c)
{

node *temp = new node;
node *cur = head;
while(cur->next && cur->next->value < c)
cur = cur->next;
  
temp->value = c;
temp->next = cur->next;
cur->next = temp;
  
}
void Print()
{
node *cur=head->next;
while(cur)
{
cout<<cur->value<<"->";
cur=cur->next;
}
cout<<"NULL"<<endl;
}
void Remove(char c)
{
node *cur=head;
while(cur->next && cur->next->value!=c)
{
cur=cur->next;
}
if(cur->next->value==c)
{
node *temp = cur->next;
cur->next = temp->next;
delete temp;
}
  
}
bool Search(char c)
{
node *cur=head->next;
while(cur)
{
if(cur->value==c)
return true;
cur=cur->next;
}
return false;
}
int Size()
{
int res=0;
node *cur=head->next;
while(cur)
{
cur=cur->next;
res++;
}
return res;
}
bool Write()
{
node *cur=head->next;
ofstream out("list.dat");
while(cur && out<<cur->value)
{
cur=cur->next;
}
if(cur)
return false;
else
return true;
}
bool Read()
{
char c;
ifstream in("list.dat");
head->next=NULL;
while(in>>c)
{
Insert(c);
}
if(head->next)
return true;
else
return false;
}
void Reverse()
{
node *cur,*temp;
cur=head->next;
head->next=NULL;
while(cur!=NULL)
{
temp=cur->next;
cur->next=head->next;
head->next=cur;
cur=temp;
}
}
void Distroy()
{
node *cur,*cur1;
cur=head->next;
head->next=NULL;
while(cur)
{
cur1=cur;
cur=cur->next;
delete cur1;
}
}
  
};
int main()
{
linked_list l;
char c,ch;
while(1)
{
cout<<"(C)lear (D)elete (G)et (I)nsert (L)ength (M)enu (P)rint (Q)uit (R)everse (S)earch (W)rite ";
cout<<"enter your choice"<<endl;
cin>>ch;
switch(ch)
{
case 'C':
l.Distroy();
break;
case 'D':

cout<<"enter the the charecter you want to delete"<<endl;
cin>>c;
l.Remove(c);
break;
case 'G':
if(l.Read())
cout<<"get sucessful"<<endl;
else
cout<<"Get failed"<<endl;
break;
case 'I':
cout<<"enter the the charecter you want to insert"<<endl;
cin>>c;
l.Insert(c);
break;
case 'L':
cout<<"length of the list = "<<l.Size();
break;
case 'P':
cout<<"the list is : ";
l.Print();
break;
case 'Q':
return 0;
break;
case 'R':
cout<<"list beore : ";
l.Print();
l.Reverse();
cout<<"List after : ";
l.Print();
break;
case 'S':

cout<<"enter the the charecter you want to search"<<endl;
cin>>c;
if(l.Search(c))
cout<<"Found"<<endl;
else
cout<<"Not found"<<endl;
break;
case 'W':
l.Write();
break;
}
}
cout << "Hello World!" << endl;
return 0;
}

Output:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote