Question: Need the code in C++ IMPLEMENTING A LINKED LIST WITH RECURSIVE FUNCTIONS INTRODUCTION The linked list operations implemented in Project 3 that involved iteration (loops)
Need the code in C++
IMPLEMENTING A LINKED LIST WITH RECURSIVE FUNCTIONS
INTRODUCTION
The linked list operations implemented in Project 3 that involved iteration (loops) can also be implemented recursively. This project modifies the previous one by carrying out those operations using recursion.
DESCRIPTION
Modify the List class in Project 3 to carry out repetitive operations recursively. These operations include:
- Dispose of the dynamic part of a List structure (the destructor).
- Re-initialize an existing List to be empty.
- Insert a value into a List in the appropriate position. If the value is already present, the List is unchanged.
- Remove a value from a List. If the value is not present, the List is unchanged.
- Return the length of a List.
- Report whether or not a particular value is present in a List.
- Return the value of the kth element of a List.
- Write out the values in a List, in order, to an output stream.
The remaining operations are unchanged.
The client program is also unchanged; only the List class is modified.
The previous code that I used was as follows:
#include
using namespace std;
//structure of node
typedef struct _node
{
int info;
struct _node *next;
}node;
//LinkedList class
class LinkedList
{
private:
node *head;
public:
//constructor
LinkedList()
{
head = nullptr;
}
//Re-initialize
void reinitialize()
{
head = nullptr;
}
//function to insert a value into a List
void insert(int val)
{
node *newnode = new node;
newnode->info = val;
if(head==nullptr)
{
newnode->next = nullptr;
head = newnode;
return;
}
node *pre, *temp = head;
if(temp->info > val)
{
newnode->next = head;
head = newnode;
return;
}
while(temp!=NULL && temp->info < val)
{
pre = temp;
temp = temp->next;
}
if(temp!=NULL && temp->info == val)
{
cout<<"The value is already present.";
return;
}
newnode->next = temp;
pre->next = newnode;
}
//function to remove a value from a List
void remove(int val)
{
node *temp, *pre;
temp = head;
if(temp->info==val)
{
head = temp->next;
temp->next = nullptr;
delete temp;
return;
}
while(temp!=nullptr && temp->info!= val)
{
pre = temp;
temp = temp->next;
}
if(temp==nullptr) return;
pre->next = temp->next;
temp->next = nullptr;
delete temp;
}
//function to check is a List empty or not
void isEmpty()
{
if(head==nullptr)
cout<<"The list is empty"< else cout<<"The list is NOT empty"< } //function to return the length of a List int length() { node *temp = head; int count = 0; while(temp!=nullptr) { temp = temp->next; count++; } return count; } //function to check whether or not a particular value is present in a List void isPresent(int val) { node *temp = head; while(temp!=nullptr && temp->info!=val) { temp = temp->next; } if(temp!=nullptr && temp->info==val) cout<<"The value "< else cout<<"The value "< } //function to return the value of the kth element of a List. int find(int k) { node *temp = head; for(int i=1; temp!=nullptr && i { temp = temp->next; } if(temp==nullptr) return -1; return temp->info; } //function to display the list void print() { node *temp = head; if(temp==nullptr) { cout<<"EMPTY LIST"; return; } cout<<"List: < "< temp = temp->next; while(temp!=nullptr){ cout<<", "< temp=temp->next; } cout<<" >"< } }; //function to display the memu void menu() { cout<<"This program responds to commands the user enters to "< cout<<"manipulate an ordered list of integers, which is "< cout<<"initially empty. In the following commands, k is a "< cout<<"position in the list, and v is an integer. "< cout<<"e -- Re-initialize the list to be empty. "< cout<<"i v -- Insert the value v into the list. "< cout<<"r v -- Remove the value v from the list. "< cout<<"m -- Is the list empty? "< cout<<"l -- Report the length of the list. "< cout<<"p v -- Is the value v present in the list? "< cout<<"k k1 -- Report the k1th value in the list. "< cout<<"w -- Write out the list. "< cout<<"h -- See this menu. "< cout<<"q -- Quit."< } //main function int main() { char ch; LinkedList list; int val, k; menu(); //while loop while(1) { cout<<"-->"; cin>>ch; //switch statement switch(ch) { case 'e': list.reinitialize(); cout< break; case 'i': cin>>val; list.insert(val); break; case 'r': cin>>val; list.remove(val); break; case 'm': list.isEmpty(); break; case 'l': val = list.length(); cout<<"The length of the list is "< break; case 'p': cin>>val; list.isPresent(val); break; case 'k': cin>>k; val = list.find(k); if(val==-1) cout<<"The list does not contain "< else cout<<"The "< break; case 'w': list.print(); break; case 'h': menu(); break; case 'q': exit(0); break; default: cout<<"Invalid command."; } } return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
