title: Linked List Deep Copy Problems
body: <p>I am having trouble with overloading the operator =. </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>template &lt;class T&gt;
  class Node
  {
  public:
    T m_data;                  // Data to be stored
    Node&lt;T&gt;* m_next;     // Pointer to the next element in the list
  
  
    // Purpose: Default constructor
    // Postconditions: next pointer set to NULL
    // ---INLINE---
    Node() : m_next(NULL) {}
  
    // Purpose: Auxiliaty constructor, construct from parameters
    // Postconditions: data and next pointer set to parameters
    // ---INLINE---
    Node(const T&amp; x, Node&lt;T&gt;* p) 
               : m_data(x), m_next(p) {}
  
  };
  
  template &lt;class T&gt;
  class LinkedList
  {
   
  public:
  
    Node&lt;T&gt;* head;     // Pointer to the head of the list
  
      // Purpose: Default constructor
      // Postconditions: head pointer set to NULL
      // ---INLINE---
    LinkedList() : head(NULL) {}
  
  // --------
  // ---- Big 3 Member Functions ---
  // --------
  
  // Purpose: Destructor
  // IMPORTANT:: For didactic purposes, the destructor is left empty.
  // YOU are expected to implement function clear() to de-allocate the list
   // --INLINE
    ~LinkedList() {}
  
  // Purpose: performs a deep copy of the data from rhs into this linked list
  // Parameters: rhs is linked list to be copied
  // Returns: *this
  // Postconditions: this list contains same data values (in the same order)
  //     as are in rhs; any memory previously used by this list has been
  //     deallocated.
    const LinkedList&lt;T&gt;&amp;  operator= (const LinkedList&lt;T&gt;&amp; rhs);
  
  // Purpose: copy constructor
  // Parameters: rhs is the LinkedList that is to be copied
  // Postconditions: this list contains same data values (in same order) 
  //      as in rhs.
    LinkedList(const LinkedList&lt;T&gt;&amp; rhs);</code></pre>
  </div>
  </div>
  </p>
  
  <p>Those are the above given. My current implementation does not provide a deep Copy.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>template&lt;class T&gt;
  const LinkedList&lt;T&gt;&amp; LinkedList&lt;T&gt;::operator =(const LinkedList&lt;T&gt;&amp; rhs)
  {
   if(this != &amp;rhs)
   {
     if(head != NULL)
     {
       clear();
     }
     Node&lt;T&gt;* current = rhs.head;
     while (current != NULL)
     {
       insert_front(current -&gt; m_data);
       current = current -&gt; m_next;
     }
     head = current;
   }  
   return(*this);
  }</code></pre>
  </div>
  </div>
  </p>