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 && i 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: < "< while(temp!=nullptr){ 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< return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
