title: Linked List: How to implement Destructor, Copy Constructor, and Copy Assignment Operator?
body: <p>This is my C++ code:</p>
  <pre><code>#include &lt;iostream&gt;
  
  using namespace std;
  
  typedef struct Node
  {   
      int data;
      Node* next;
  }Node;
  
  class LinkedList
  {
  private:
      Node* first;
      Node* last;
  public:
      LinkedList() {first = last = NULL;};
      LinkedList(int A[], int num);
      ~LinkedList();
  
      void Display();
      void Merge(LinkedList&amp; b);
    
  };
  
  // Create Linked List using Array
  LinkedList::LinkedList(int A[], int n)
  {   
      Node* t = new Node;
      if (t == NULL)
      {
          cout &lt;&lt; &quot;Failed allocating memory!&quot; &lt;&lt; endl;
          exit(1);
      }
      t-&gt;data = A[0];
      t-&gt;next = NULL;
      first = last = t;
  
      for (int i = 1; i &lt; n; i++)
      {
          t = new Node;
          if (t == NULL)
          {
              cout &lt;&lt; &quot;Failed allocating memory!&quot; &lt;&lt; endl;
              exit(1);
          }
          t-&gt;data = A[i];
          t-&gt;next = NULL;
          
          last-&gt;next = t;
          last = t;
      }
  }
  
  // Deleting all Node in Linked List
  LinkedList::~LinkedList()
  {
      Node* p = first;
      Node* tmp;
  
      while (p != NULL)
      {
          tmp = p;
          p = p-&gt;next;
          delete tmp;
      }
  }
  
  // Displaying Linked List
  void LinkedList::Display()
  {
      Node* tmp;
  
      for (tmp = first; tmp != NULL; tmp = tmp-&gt;next)
          cout &lt;&lt; tmp-&gt;data &lt;&lt; &quot; &quot;;
      cout &lt;&lt; endl;    
  }
  
  // Merge two linked list
  void LinkedList::Merge(LinkedList&amp; b)
  {
      // Store first pointer of Second Linked List
      Node* second = b.first;
      Node* third = NULL, *tmp = NULL;
  
      // We find first Node outside loop, smaller number, so Third pointer will store the first Node
      // Then, we can only use tmp pointer for repeating process inside While loop
      if (first-&gt;data &lt; second-&gt;data)
      {
          third = tmp = first;
          first = first-&gt;next;
          tmp-&gt;next = NULL;
      }
      else
      {
          third = tmp = second;
          second = second-&gt;next;
          tmp-&gt;next = NULL;
      }
  
      // Use while loop for repeating process until First or Second hit NULL
      while (first != NULL &amp;&amp; second != NULL)
      {
          // If first Node data is smaller than second Node data
          if (first-&gt;data &lt; second-&gt;data)
          {
              tmp-&gt;next = first;
              tmp = first;
              first = first-&gt;next;
              tmp-&gt;next = NULL;
          }
          // If first Node data is greater than second Node data
          else
          {
              tmp-&gt;next = second;
              tmp = second;
              second = second-&gt;next;
              tmp-&gt;next = NULL;
          }
      }
  
      // Handle remaining Node that hasn't pointed by Last after while loop
      if (first != NULL)
          tmp-&gt;next = first;
      else
          tmp-&gt;next = second;
  
      // Change first to what Third pointing at, which is First Node
      first = third;    
  
      // Change last pointer from old first linked list to new last Node, after Merge
      Node* p = first;
      while (p-&gt;next != NULL)
      {
          p = p-&gt;next;
      }    
      last = p;
      
      // Destroy second linked list because every Node it's now connect with first linked list
      // This also prevent from Double free()
      b.last = NULL;
      b.first = NULL;
  }
  
  int main()
  {
      int arr1[] = {4, 8, 12, 14, 15, 20, 26, 28, 30};
      int arr2[] = {2, 6, 10, 16, 18, 22, 24};
      int size1 = sizeof(arr1) / sizeof(arr1[0]);
      int size2 = sizeof(arr2) / sizeof(arr2[0]);
      
      LinkedList l1(arr1, size1);
      LinkedList l2(arr2, size2);
  
      l1.Display();
      l2.Display();
      
      // Merge two linked list, pass l2 as reference
      l1.Merge(l2);
      l1.Display();
  
      return 0;
  }
  </code></pre>
  <p>I'm beginner on C++ and in this code, I practice how to Merge two linked list. This actually works perfectly. I've successfully Merge the two Linked List in sorted order.</p>
  <p>But, there's people said that I should've follow <em>the Rule of Three</em> on C++. Which implement: <strong>Destructor</strong>, <strong>Copy Constructor</strong>, and <strong>Copy Assignment Operator</strong>.</p>
  <p>I've watched many videos about that. I do understand that is basically handle <strong>Shallow Copy</strong> especially when we don't want two different object point to the same address of memory. But, for my problem is, I still don't know how to Implement it on a Class that working on a Linked List just like my code above.</p>
  <p>Someone said, in my <code>main()</code>, this code: <code>l1.Merge(l2);</code> is somehow incorrect because I don't have explicit Copy Constructor.</p>
  <p>And if you look at my <code>Merge()</code> function, in Last line, if I didn't to this: <code>b.last = NULL;</code> and <code>b.first = NULL;</code> , which simply destroy pointer of Second Linked list, the Compiler give me warning: <strong>Double free()</strong> detected.</p>
  <p>So, I think my question is:</p>
  <ol>
  <li>How can this code: <code>l1.Merge(l2);</code> is have something to do with Copy Constructor?</li>
  <li>Is <code>Double free()</code> happened because I don't implement the Rule of Three? If yes, how to address them?</li>
  <li>How to write the Rule of Three based on my code? When or How to use them?</li>
  <li>Based on this Code, is there something wrong? Do I still need the Rule of Three if my Program only want to Merge Linked List?</li>
  </ol>
  <p>Thank You. I hope someone can explain to me like I'm 10 years old. and hope someone can write me some Code.</p>