title: Error when trying to delete entities within a linked list
body: <p>I'm working through an assignment for one of my classes, where we have to implement two functions, one that creates an identical linked list that is a copy of the given one, and another that deletes all elements within the list and sets the head pointer to NULL. When I run my code, I either somehow get trapped within a loop, or I get an error message about a Segmentation fault.</p>
  <pre><code>#ifndef OLLIST_H
  #define OLLIST_H
  
  typedef int ListItem;
  
  struct Node {
    ListItem item;
    Node *next;
  };
  
  class OLList {
  public:
    OLList(); // PROMISES: Creates empty list.
    OLList(const OLList&amp; source);
    OLList&amp; operator =(const OLList&amp; rhs);
    ~OLList();
  
    void insert(const ListItem&amp; itemA);
    // PROMISES:
    //    A node with a copy of itemA is added in 
    //    a way that preserves the nondecreasing 
    //    order of items in nodes.
  
    void remove(const ListItem&amp; itemA);
    //  PROMISES:
    //    If no node has an item matching itemA, 
    //    list is unchanged.
    //    Otherwise exactly one node with 
    //    its item == itemA is removed.
  
    void print() const;
    // PROMISES:
    //   Prints items in list on a single line, with commas separating
    //   the items and square brackets marking the ends of the list.
    // NOTE:
    //   This is different from the print function presented in lectures.
  
  private:
    Node *headM;
    void destroy(); 
    // Deallocate all nodes, set headM to zero.
  
    void copy(const OLList&amp; source); 
    // List becomes copy of source.
  };
  #endif
  
  </code></pre>
  <p>within this code segment, the functions that have to be completed are <code>copy()</code> and <code>destroy()</code>. My function implementation is as follows:</p>
  <pre><code>OLList&amp; OLList::operator =(const OLList&amp; rhs)
  {
      if (this != &amp;rhs) {
          destroy();
          copy(rhs);
      }
      return *this;
  }
  
  void OLList::destroy()
  {
      struct Node *temp;
      
      while (headM != NULL) {
          temp = headM;
          headM = headM -&gt;next;
          delete temp;
      }
      
      headM = NULL;
  }
  
  void OLList::copy(const OLList&amp; source)
  {
      // this function is not properly designed. As part of the exercise A
      // students are supposed to remove the folloiwng lines and
      // complete the definition of this helper function.
      if(source.headM == nullptr) {
          cout &lt;&lt; &quot;\nCannot copy because source list is empty.&quot;;
          return;
      }
      else {
          this-&gt;headM = new Node;
          this-&gt;headM-&gt;item = source.headM-&gt;item;
          Node *travel_ptr = source.headM-&gt;next;
          Node *temp_ptr = this-&gt;headM;
          Node *copy_ptr = 0;
      
          while (travel_ptr != 0) {
              copy_ptr = new Node;
              copy_ptr-&gt;item = travel_ptr-&gt;item;
              temp_ptr-&gt;next = copy_ptr;
              travel_ptr = travel_ptr-&gt;next;
              temp_ptr = temp_ptr-&gt;next;
          } 
      }
  }
  
  </code></pre>
  <p>In order to figure out what was going wrong, I printed to the console and found that everything worked fine, until the assignment operator is called within this section of code in the <code>main()</code> function. For some reason, when the function tries to cycle through the nodes and delete them, the only one that works is the first iteration, and all the subsequent nodes fail to be deleted.</p>
  <pre><code>int main()  {
      
      OLList the_list;
      
      cout &lt;&lt; &quot;List just after creation. expected to be [ ]&quot; &lt;&lt; endl;
  
      the_list.insert(330);
      the_list.insert(440);
      the_list.insert(220);
      the_list.insert(110);
      the_list.insert(550);
      the_list.insert(99);
      the_list.insert(120);
  
      cout &lt;&lt; &quot;the_list after some insertions. Expected to be: [ 99, 110, 120, 220, 330, 440, 550 ]&quot;&lt;&lt; endl;
  
      the_list.print();
  
      OLList other_list, third_list;
  
  cout &lt;&lt; &quot;chaining assignment operator ...&quot;&lt;&lt; endl;
      third_list = other_list = the_list;
      cout &lt;&lt; &quot;the_list after chaining assignment operator: expected to be: [ 99, 110, 120, 220, 440 ]&quot; &lt;&lt; endl;
      the_list.print();
      cout &lt;&lt; &quot;other_list after chaining: expected to be: [ 99, 110, 120, 220, 440 ]&quot; &lt;&lt; endl;
      other_list.print();
      cout &lt;&lt; &quot;third_list after chaining: expected to be: [ 99, 110, 120, 220, 440 ]&quot; &lt;&lt; endl;
      third_list.print();
  
      return 0;
  }
  </code></pre>