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

I have an error on this code but i could not find it there is a memory leak some

ID: 3726565 • Letter: I

Question

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; }

Explanation / Answer

/******************* I have updated the Code . There were two issue first in void delete_nodes (void) method. and i had to rewrite the remove method again as there were lot mistakes. Please go through the code . It works perfectly. I have tested the main program which you have provided **************/

// 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 <iostream>
using namespace std;
int number_instantiated = 0;

template <class T>
class Node {
public:
Node (const T &value, Node<T> *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<T>* next () const { return next_; }
void next (Node<T> *new_next) { next_ = new_next; };
const T& value () const { return value_; }
void value (const T &value) { value_ = value; }

private:
Node ();
T value_;
Node<T> *next_;
};

template <class T>
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<T>(new_item, head_)) != 0) ? 0 : -1;
}

// returns 0 on success, -1 on failure
int remove (const T &item_to_remove) {
    Node<T> *marker = head_;
    Node<T> *temp = 0; // temp points to one behind as we iterate
   
    if(marker->value() == item_to_remove){
    cout<<"Deleting first Node"<<marker->value()<<endl;
    temp = marker;
    if(marker->next() == 0){
      head_ = 0;
            delete marker; // marker is the only element in the list
            marker = 0;
  }else{
  
      marker = marker->next();
      head_ = marker;
      cout<<"After deleting first Node "<<marker->value();
      cout<<"After deleting Head Value is "<<head_->value();
   
      delete temp;
    }
    return 0;
}


while(marker !=0 ){
  if(marker->value() == item_to_remove){
   temp->next( marker->next() );
   //marker->next(0);
   delete marker;
   return 0;
  }
  
  temp = marker;
  marker = marker->next();
}

    return -1; // failure
}

void print (void) {
    Node<T> *marker = head_;
    if(marker == 0){
    cout<<"Head is NULL"<<endl;
}
    while (marker != 0) {
      cout << marker->value() << endl;
      marker = marker->next();
    }
}

private:
void delete_nodes (void) {
    Node<T> *marker = head_;
    while (marker != 0) {
      Node<T> *temp = marker;
      marker = temp->next();
      delete temp;
     
    }
}
       
Node<T> *head_;
};

int main (int argc, char **argv) {
LinkedList<int> *list = new LinkedList<int> ();

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 ();

//delete list;

return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote