Question: C++ need help free memory leak, I am trying to print out a linked list from the beginning to the end and from the back

C++ need help free memory leak, I am trying to print out a linked list from the beginning to the end and from the back to the beginning, but I could not free those memory leack that caused by printing the linked list reversly. I tried to provid all code by the chegg does not allow me since the program is too long

#include using namespace std; #include "List.h"

List::List(){ head=NULL; }

void List::add(Book* book){ Node* tmpNode; Node* currNode; Node* prevNode;

tmpNode =new Node; tmpNode->data = book; tmpNode->next =NULL;

currNode = head; prevNode = NULL;

while(currNode !=NULL){ if(book->lessThan(currNode->data)){ break; } prevNode=currNode; currNode=currNode->next; } if(prevNode ==NULL){ head=tmpNode; } else{ prevNode->next =tmpNode; } tmpNode->next =currNode; }

void List::print(){ Node* currNode=head; while(currNode !=NULL){ currNode->data->print(); currNode=currNode->next; } printReverse(); }

void List::printReverse(){ Node* currNode=head; printReverseRecursive(currNode); delete currNode; } void List::printReverseRecursive(Node* head){ if(head){ printReverseRecursive(head->next); head->data->print(); } }

List::~List(){ Node* currNode, *nextNode; currNode=head; while(currNode != NULL){ nextNode=currNode->next; delete currNode; currNode=nextNode; } }

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!