Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have an error on this code but i could not find it there is a memory leak somewhere in the program. ( It is

I have an error on this code but i could not find it there is a memory leak somewhere in the program. ( It is in the LinkedList::remove() function. One of the cases for remove doesn't work properly.) any one can help me ?

// main.cc // Andrew Gilpin // agg1@cec.wustl.edu // This file contains the example program used in the gdb debugging // tutorial. The tutorial can be found on the web at // http://students.cec.wustl.edu/~agg1/tutorial/ #include int number_instantiated = 0; template class Node { public: Node (const T &value, Node *next = 0) : value_(value), next_(next) { cout << "Creating Node, " << ++number_instantiated << " are in existence right now" << endl; } ~Node () { cout << "Destroying Node, " << --number_instantiated << " are in existence right now" << endl; next_ = 0; } Node* next () const { return next_; } void next (Node *new_next) { next_ = new_next; }; const T& value () const { return value_; } void value (const T &value) { value_ = value; } private: Node (); T value_; Node *next_; }; template class LinkedList { public: LinkedList () : head_(0) {}; ~LinkedList () { delete_nodes (); };

// returns 0 on success, -1 on failure int insert (const T &new_item) { return ((head_ = new Node(new_item, head_)) != 0) ? 0 : -1; } // returns 0 on success, -1 on failure int

remove (const T &item_to_remove) { Node *marker = head_; Node *temp = 0; // temp points to one behind as we iterate

while (marker != 0) {

if (marker->value() == item_to_remove){

if (temp == 0) {

// marker is the first element in the list

if (marker->next() == 0) {

head_ = 0;

delete marker; // marker is the only element in the list

marker = 0;

} else {

head_ = new Node(marker->value(), marker->next());

delete marker;

marker = 0;

} return 0;

} else {

temp->next (marker->next());

delete temp;

temp = 0;

return 0;

}

} temp = marker; marker = marker->next();

} return -1; // failure

} void print (void) { Node *marker = head_; while (marker != 0) { cout << marker->value() << endl; marker = marker->next(); } } private: void delete_nodes (void) { Node *marker = head_; while (marker != 0) { Node *temp = marker; delete marker; marker = temp->next(); } } Node *head_; }; int main (int argc, char **argv) { LinkedList *list = new LinkedList (); list->insert (1); list->insert (2); list->insert (3); list->insert (4); cout << "The fully created list is:" << endl; list->print (); cout << endl << "Now removing elements:" << endl; list->remove (4); list->print (); cout << endl; list->remove (1); list->print (); cout << endl; list->remove (2); list->print (); cout << endl; list->remove (3); list->print (); //cout << endl; // me** delete list; return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started