I need help writing this program in c++. Thank you in advance. Write a class tha
ID: 3665855 • Letter: I
Question
I need help writing this program in c++. Thank you in advance.
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:
Constructor - initializes head to null.
Destructor - responsible for deallocating the list from memory
Insert - Accepts a character argument, inserts a node containing the character into the linked list. Insert preserves the order of the linked list. THe linked list orderes the nodes in ascending order. For example, Head -> A -> E -> O -> U -> NULL.
remove - Accepts a charater argument, then searches the linked list for the first matching node and removes it. Does nothing if a matching node is not found.
search -- Accepts a character argument, then searches for a matching node in the linked list. Returns true if found, false otherwise.
print -- displays the contents of the linked list to the screen.
size -- returns the number of nodes with in the linked list.
write -- writes the contents of the linked list to a file named "list.dat". Returns true if the file is successfully written, false othewise. Each nodes value should be written to the file without whitespace. For example, the linked listmentioned in the insert method would appear as AEOU within the file.
read -- read the "list.dat" file, and constructs a linked list using the data. Returns true if linked list is succesfully created, false otherwise.
destroy -- removes the linked list from memory. Could be called by the destructor.
The extra credit methods are:
reverse -- accepts no arguments, merely calls the overloaded reverse method.
reverse -- accepts a node pointer as an argument. Uses recursion to display the contents of the linked list in reverse order. For example, the list mentioned in the insert description would be displayed as : Head-> U -> O -> E -> A -> NULL
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.
OutPut:
LinkedList Embedded Node structure +value: char +Next: Node* -head: Node* reverse n Node*) void uer LinkedList() LinkedList() +insert( v char) void +remove( v char void +search v vchar) boo queryh Extra credit B +print() void query methods +size() int query) write() bool read() bool +destroy0 void +reverse() void fqueryExplanation / Answer
From based on Question
Answer
# include <iostream.h>
# include <process.h>
# include <conio.h>
# include <malloc.h>
int cur_link_list=1;
int display_menu();
struct link_list
{
//int no;
char value;
struct link_list *next;
};
//Here Creating Linked ListExample
class LinkedList
{
link_list *list;
public:
link_list *head;
//Constructor
LinkedList()
{
list=NULL;
head=NULL;
}
//Destructor
~LinkedList(){
}
//read method
bool read();
//print list data
void print();
//insert data
void insert(char v);
//delete or destroy
void destroy();
//reverse methdo
void reverse(void);
};
void LinkedList :: read()
{
char value;
//int no;
list=head;
while(list->next!=NULL)
{
list=list->next;
}
while(1)
{
cout<<"Enter character :";
cin>>value;
if(value!='')
{
if(list==NULL)
{
list=new link_list;
head=list;
}
list->value=value;
list->next = new link_list;
list=list->next;
}
else
{
list->next=NULL;
break;
}
}
}
void LinkedList :: print()
{
list=head;
cout<<endl;
if (list==NULL)
{
cout<<"Link list is empty !!!";
return;
}
while(list->next!=NULL)
{
cout<<list->value<<" ";
list=list->next;
}
}
void LinkedList :: insert(char v)
{
int ch;
list=head;
cout<<endl;
cout<<"[ 1 ] : Insert at First"<<endl;
cout<<"[ 2 ] : Insert in Middle"<<endl;
cout<<"[ 3 ] : Insert at Last"<<endl;
cout<<"[ 4 ] : Back to main Menu"<<endl;
cout<<"Enter your choice :";
cin>>ch;
link_list *newnode;
newnode=new link_list;
switch(ch)
{
case 1:
cout<<"Enter Value:";
cin>>newnode->no;
list=head;
if(list==NULL)
{
list=newnode;
newnode->next=NULL;
head=list;
}
else
{
newnode->next=list;
head=newnode;
}
break;
case 2: char value;
cout<<endl;
cout<<"Enter Value after which you want to insert :";
cin>>value;
list=head;
while(list->next !=NULL)
{
if(list->value==value)
{
cout<<"Enter Valueto Insert :";
cin>>newnode->value;
newnode->next=list->next;
list->next=newnode;
if(list==head)
{
head=newnode;
}
return;
}
list=list->next;
}
cout<<"Key not found ..."<<endl;
break;
case 3 : list=head;
while(list->next!=NULL)
{
list=list->next;
}
cout<<"Enter Value:";
cin>>newnode->value;
if(head==NULL)
{
list=newnode;
head=list;
}
else
{
list->next=newnode;
newnode->next=NULL;
}
break;
}
}
void LinkedList :: destroy()
{
cout<<endl;
list=head;
char value;
cout<<"Enter the Valueto deleted :";
cin>>value;
if(head->value==value)
{
head=head->next;
return;
}
while(list->next!=NULL)
{
if(list->next->value==value)
{
list->next=list->next->next;
return;
}
list=list->next;
}
cout<<"Valuenot not found !!!";
}
void LinkedList :: reverse()
{
int a[50];
char value[50];
list=head;
int i=0;
while(list->next!=NULL)
{
a[i]=list->value;
list=list->next;
i=i+1;
}
int n=i-1;
i=n;
list=head;
while(list->next!=NULL)
{
list->value=a[i];
list=list->next;
i=i-1;
}
}
void main()
{
clrscr();
//Object creation
LinkedList l1,l2,l3;
while(1)
{
switch(display_menu())
{
case '01': cout<<"Enter LinkList Valuelike [A,B,C... Characters]:";
int n;
char v;
cin>>v;
if(n>=1 && n<=3)
{
cur_link_list=n;
}
break;
case 2: switch(cur_link_list)
{
case 1: l1.read();
break;
case 2: l2.read();
break;
case 3: l3.read();
break;
}
getch();
break;
case 'I': switch(cur_link_list)
{
case 1 : l1.insert(char v);
break;
case 2 : l2.insert(char v);
break;
case 3 : l3.insert(char v);
break;
}
getch();
break;
case 'P': switch(cur_link_list)
{
case 1: l1.print();
break;
case 2: l2.print();
break;
case 3: l3.print();
break;
}
getch();
break;
case 'D':
switch(cur_link_list)
{
case 1: l1.print();
l1.destroy();
break;
case 2: l2.print();
l2.destroy();
break;
case 3: l3.print();
l3.destroy();
break;
}
getch();
break;
case 'R': switch(cur_link_list)
{
case 1: l1.reverse();
break;
case 2: l2.reverse();
break;
case 3: l3.reverse();
break;
}
getch();
break;
case 7 : exit(1);
}
}
}
int display_menu()
{
clrscr();
cout<<endl;
cout<<" ( 01 )Select Linklist (Selected List is:"<<cur_link_list<<")"<<endl;
cout<<" ( D ) Delete"<<endl;
cout<<" ( G ) Get"<<endl;
cout<<" ( I ) Insert"<<endl;
cout<<" ( P ) Print"<<endl;
cout<<" ( R ) Reverse"<<endl;
cout<<" ( 0 ) Exit"<<endl;
cout<<" Enter your choice :";
int ch;
cin>>ch;
return ch;
}
Output Screen