title: Linked list destructor
body: <p>I am working on a Linked List class.  Whenever I compile, I get no errors or warnings.  However, the executable stops working (using Windows Dev C++).  I have deduced that the problem is my destructor.  </p>
  
  <p>My understanding is the destructor gets called with the delete keyword for a dynamically created object, or whenever the object goes out of scope for an object on the stack.</p>
  
  <p>I think my problem occurs when my stack object (Node* result) calls the destructor, but not really sure.</p>
  
  <p>Here's my header file:</p>
  
  <pre><code>    #ifndef Node_H
      #define Node_H
  
      class Node{
            int data;
            Node* next;
  
      public:
             Node(int data);       
             ~Node();
             void insert(int d);
             Node* remove(int d);
             void removeDups(void);
             Node* add(Node* a, Node* b);
             void printOut(void);
      };
  
      #endif
  </code></pre>
  
  <p>Revelant parts of .cpp file:</p>
  
  <pre><code>    #include &lt;iostream&gt;
      #include "Node.h"
  
      using namespace std;
  
      Node::Node(int d){
            data = d;
            next = NULL;
            }
  
      Node::~Node(){
             Node* n = this;
             Node* delptr;
  
             while(n-&gt;next != NULL){
                           delptr = n;
                           n = n-&gt;next;
                           delete delptr;
                           }
             delete n;            
  
          }
  
      void Node::insert(int d){
  
            Node* n = this;
            Node* current = new Node(d);
  
            while(n-&gt;next != NULL){
                    n = n-&gt;next;                   
                    }
  
            n-&gt;next = current;
            }
  </code></pre>
  
  <p>Main:</p>
  
  <pre><code>    int main (void){
          int i = 0;
  
          Node* root = new Node(111);
          Node* result;
  
          root-&gt;printOut();
  
          for (i = 0; i &lt; 11; i++){
              root-&gt;insert(i);
              } 
  
          root-&gt;printOut();
  
          delete root; 
          getchar();
          return 0;   
      }
  </code></pre>
  
  <p>Also, I am running Dev C++ on a USB.  My hope with that was to prevent memory leaks on my OS.  Is that correct?</p>
  
  <p>Thanks</p>
  
  <p>-- Tring Vu</p>