Question: Please rewrite this function using recursive function -------------------------------------------------------- // This function is from linkedlist.cpp bool LinkedList::del(char ch) { Node cur = head, prev, *next =

Please rewrite this function using recursive function

--------------------------------------------------------

// This function is from linkedlist.cpp

bool LinkedList::del(char ch)

{

Node cur = head, prev, *next = NULL,*tmp;

int found = 0;

if (head->ch == ch) //if ch found at head

{

tmp = head;

head = head->next;

delete tmp;

return true;

}

prev = cur;

while (cur->next != NULL)

{

prev = cur;

next = cur->next->next;

if (cur->next->ch == ch)

{

found = 1;

break;

}

cur = cur->next;

}

if (!found)

{

cout << "Given ch is not in the list" << endl;

return false;

}

else

{

tmp = prev->next;

prev->next = next;

free(tmp);

}

return true;

}

-------------------------------------------------------------

And all the code is given below

------------------------------------------------------------

//linkedlist.h

#ifndef LINKED_LIST

#define LINKED_LIST

#include

struct Node

{

char ch;

Node *next;

};

class LinkedList

{

Node *head;

public:

LinkedList();

~LinkedList();

void add(char ch);

bool find(char ch);

bool del(char ch);

friend std::ostream& operator<<(std::ostream& out, LinkedList& list);

};

#endif // LINKED_LIST

----------------------------------------------------------

//linkedlist.cpp

#include"linkedlist.h"

#include

using namespace std;

LinkedList::LinkedList()

{

head = NULL;

}

LinkedList::~LinkedList()

{

Node *cur = head,*tmp;

while (cur != NULL)

{

tmp = cur->next;

delete cur;

cur = tmp;

}

}

void LinkedList::add(char ch)

{

Node *cur = head,*newNode;

newNode = new Node;

newNode->ch = ch;

newNode->next = NULL;

if (head == NULL)

{

head = newNode;

}

else

{

while (cur->next != NULL)

cur = cur->next;

cur->next = newNode;

}

}

bool LinkedList::find(char ch)

{

Node *cur = head;

bool found = false;

while (cur != NULL)

{

if (cur->ch == ch)

{

found = true;

break;

}

cur = cur->next;

}

return found;

}

bool LinkedList::del(char ch)

{

Node cur = head, prev, *next = NULL,*tmp;

int found = 0;

if (head->ch == ch) //if ch found at head

{

tmp = head;

head = head->next;

delete tmp;

return true;

}

prev = cur;

while (cur->next != NULL)

{

prev = cur;

next = cur->next->next;

if (cur->next->ch == ch)

{

found = 1;

break;

}

cur = cur->next;

}

if (!found)

{

cout << "Given ch is not in the list" << endl;

return false;

}

else

{

tmp = prev->next;

prev->next = next;

free(tmp);

}

return true;

}

std::ostream& operator<<(std::ostream& out, LinkedList& list)

{

Node *cur = list.head;

out << "List contains: ";

while (cur != NULL)

{

out << cur->ch << " ";

cur = cur->next;

}

out << " ";

return out;

}

----------------------------------

//app.cpp

#include

#include "linkedlist.h"

using namespace std;

void find(LinkedList& list, char ch)

{

if (list.find(ch))

cout << "found ";

else

cout << "did not find ";

cout << ch << endl;

}

int main()

{

LinkedList list;

list.add('x');

list.add('y');

list.add('z');

cout << list;

find(list, 'y');

list.del('y');

cout << list;

find(list, 'y');

list.del('x');

cout << list;

find(list, 'y');

list.del('z');

cout << list;

find(list, 'y');

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!