Question: C++ Language. Kindly help me with this program. TITLE IMPLEMENTING A LINKED LIST WITH RECURSIVE FUNCTIONS INTRODUCTION The linked list operations implemented in Project 3

C++ Language. Kindly help me with this program.

TITLE

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.

INPUT

As in Project 3

OUTPUT

As in Project 3

ERRORS

As in Project 3

EXAMPLE

As in Project 3

OTHER REQUIREMENTS

As in Project 3

HINTS

The smaller problem is always the "tail" of the List.

Code used in Project 3 is:

#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"<

//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 "<

//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 && inext; }

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: < "<info; temp = temp->next;

while(temp!=nullptr){ cout<<", "<info; temp=temp->next; } cout<<" >"<

//function to display the memu void menu() { cout<<"This program responds to commands the user enters to "<

//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<>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 "<>val; list.isPresent(val); break; case 'k': cin>>k; val = list.find(k); if(val==-1) cout<<"The list does not contain "<

return 0; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!