title: Assignment operator in linked list C++
body: <p>I'm trying to implement linked list in c++. </p>
  
  <p>I implement my assignment operator like this:</p>
  
  <pre><code>// assignment operator
  template&lt;class T&gt;
  LinkedList&lt;T&gt;&amp; LinkedList&lt;T&gt;::operator = (const LinkedList&amp; rhs) {
  
  if (&amp;rhs != this) {
      Node *tmp = head;
  
      while (tmp -&gt; next) {
          head = head -&gt; next;
          delete tmp;
          tmp = head;
      }
  
      tmp = rhs -&gt; head;
  
      while (tmp) {
          append(tmp);
          tmp = tmp -&gt; next;
      }
  }
  
      return *this;
  }
  </code></pre>
  
  <p>In my main function, i use the following code to test:</p>
  
  <pre><code>LinkedList&lt;int&gt; *lst1 = new LinkedList&lt;int&gt;(7);
  
  LinkedList&lt;int&gt; *lst2 = new LinkedList&lt;int&gt;(101);
  
  std::cout &lt;&lt; lst1 -&gt; head -&gt; data &lt;&lt; std::endl;
  std::cout &lt;&lt; lst2 -&gt; head -&gt; data &lt;&lt; std::endl;
  
  lst1 = lst2;
  
  std::cout &lt;&lt; lst1 -&gt; head -&gt; data &lt;&lt; std::endl;
  
  delete lst1;
  delete lst2;            &lt;--------------- error here
  </code></pre>
  
  <p>As I expect, the console outputs:</p>
  
  <blockquote>
    <p>7 101 101</p>
  </blockquote>
  
  <p>But when the program try to delete lst2, i get an error saying:</p>
  
  <blockquote>
    <p>pointer being freed was not allocated</p>
  </blockquote>
  
  <p>I use debugger and find out when the program is doing assignment:</p>
  
  <pre><code>lst1 = lst2;
  </code></pre>
  
  <p>lst1 is actually referring to the address that points lst2 instead of getting a copy of lst2, so when i delete lst1, lst2 is already gone.</p>
  
  <p><strong>So can anyone please tell me what is wrong with my assignment operator?</strong></p>
  
  <p>I'm sorry if this is a novice question but I've been spending a few hours and could not figure out.</p>
  
  <p><strong><em>My completed code is shown below:</em></strong></p>
  
  <pre><code>template&lt;class T&gt;
  class LinkedList {
  
  private:
      class Node {
      public:
          T data;
          Node *next;
  
          // default constructor
          Node() = default;
  
          // constructor with data
          Node(const T&amp; data) : data(data), next(NULL) {}
  
      };
  
  public:
      Node *head;
  
      LinkedList(const LinkedList&amp; copyLst);
      LinkedList&amp; operator=(const LinkedList&amp; byValList);
      LinkedList() : head(NULL){}
      LinkedList(Node *newNode) : head(newNode) {}
      LinkedList(T val) {
          head = new Node(val);
      }
      ~LinkedList();
  
      static LinkedList&lt;int&gt; sumLists(const LinkedList&lt;int&gt;&amp; lst1, const LinkedList&lt;int&gt;&amp; lst2) ;
  
      void insertAtFront(T val);
      void insertAtEnd(T val);
      void printList();
      void insert(T val);
  
      void append(const Node&amp;);
  };
  
  // copy constructor
  template&lt;class T&gt;
  LinkedList&lt;T&gt;::LinkedList(const LinkedList&lt;T&gt;&amp; copyLst) {
  
      const Node *cpCurrent = copyLst.head;
      Node *lsCurrent = NULL;
  
      if (cpCurrent != NULL) {
          head = new Node(cpCurrent -&gt; data);
          lsCurrent = head;
  
          cpCurrent = cpCurrent -&gt; next;
  
      }
  
      while (cpCurrent != NULL) {
          Node *newNode = new Node(cpCurrent -&gt; data);
          lsCurrent -&gt; next = newNode;
  
          lsCurrent = lsCurrent -&gt; next;
          cpCurrent = cpCurrent -&gt; next;
      }
  }
  
  // assignment operator
  template&lt;class T&gt;
  LinkedList&lt;T&gt;&amp; LinkedList&lt;T&gt;::operator = (const LinkedList&amp; rhs) {
  
      if (&amp;rhs != this) {
          Node *tmp = head;
  
          while (tmp -&gt; next) {
              head = head -&gt; next;
              delete tmp;
              tmp = head;
          }
  
          tmp = rhs -&gt; head;
  
          while (tmp) {
              append(tmp);
              tmp = tmp -&gt; next;
          }
      }
  
      return *this;
  }
  
  // destructor
  template&lt;class T&gt;
  LinkedList&lt;T&gt;::~LinkedList() {
      Node *current = head;
  
      while (current != NULL) {
          head = head -&gt; next;
          delete current;
          current = head;
      }
  }
  
  template&lt;typename T&gt;
  void LinkedList&lt;T&gt;::append(const Node&amp; node ){
  
      if (NULL == head) {
          Node *newNode = new Node(node -&gt; data);
          head = newNode;
      } else {
          Node *current = head;
  
          while (current -&gt; next) {
              current = current -&gt; next;
          }
  
  
          Node *newNode = new Node(node -&gt; data);
          current -&gt; next = newNode;
      }
  }
  </code></pre>