Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

/ Q6: Remove Book (15 points) // This function will be used to remove a book fro

ID: 3738404 • Letter: #

Question

/ Q6: Remove Book (15 points) // This function will be used to remove a book from the list.

// Traverse the list and use the parameters to remove the book.

// You must remove all the elements in the buys linked list.

void remove_book(string title, string author)

{ Container *temp = list; Container *temp2 = list; Buy *tempPlace;

if (list->book->getTitle() == title && list->book->getAuthor() == author)

{ while(list->book->buys != NULL)

{ tempPlace = list->book->buys;

list->book->buys = list->book->buys->next;

delete(tempPlace); } list = list->next;

delete(temp->book); delete(temp); }

else

{ temp = list->next;

while (temp2->next != NULL)

{ if (temp->book->getTitle().compare(title) == 0)

{ while (temp->book->buys != NULL)

{ tempPlace = temp->book->buys;

temp->book->buys = temp->book->buys->next; delete(tempPlace); }

temp2->next = temp->next;

delete(temp->book);

delete(temp); break; }

temp2 = temp;

temp = temp->next; }

} }

Can someone help me make this code recursive please?

Explanation / Answer

I am providing you the sample code for recursively deletion of a node in the linked list.

We recursively reduce value of k. When k reaches 1, we delete current node and return next of current node as new node. When function returns, we link the returned node as next of previous node.

#include <bits/stdc++.h>

using namespace std;

struct Node {

    int data;

    struct Node* next;

};

Node* deleteNode(Node* start, int n)

{

    if (n < 1)

       return start;

    if (start == NULL)

       return NULL;

    if (n == 1)

    {

        Node *res = start->next;

        delete(start);

        return res;

    }

    start->next = deleteNode(start->next, n-1);

    return start;

}