title: Implementing a doubly linked list copy constructor in C++
body: <p>I'm having trouble figuring out how to implement a copy constructor for doubly linked lists. The code I have so far is incorrect, I believe, because I lose track of the header node, but I'm not sure how to rectify this. Any help would be much appreciated!</p>
  
  <p>DoublyLinkedList.h</p>
  
  <pre><code>#include &lt;cstdlib&gt;
  #include &lt;iostream&gt;
  using namespace std;
  class DoublyLinkedList; // class declaration
  
  // list node
  class DListNode {
  private: 
      int obj;
      DListNode *prev, *next;
      friend class DoublyLinkedList;
  public:
      DListNode(int e = 0, DListNode *p = NULL, DListNode *n = NULL)
          : obj(e), prev(p), next(n) {}
  
      int getElem() const { return obj; }
      DListNode * getNext() const { return next; }
      DListNode * getPrev() const { return prev; }
  };
  
  // doubly linked list
  class DoublyLinkedList {
  protected: 
      DListNode header, trailer;
  public:
      DoublyLinkedList() : header(0), trailer(0) // constructor
          { header.next = &amp;trailer; trailer.prev = &amp;header; }
      DoublyLinkedList(const DoublyLinkedList&amp; dll); // copy constructor
      ~DoublyLinkedList(); // destructor
      DoublyLinkedList&amp; operator=(const DoublyLinkedList&amp; dll); // assignment operator
  
  
      DListNode *getFirst() const { return header.next; } // return the pointer to the first node
      const DListNode *getAfterLast() const { return &amp;trailer; }  // return the pointer to the trailer
      bool isEmpty() const { return header.next == &amp;trailer; } // return if the list is empty
      int first() const; // return the first object
      int last() const; // return the last object
  
      void insertFirst(int newobj); // insert to the first of the list
      int removeFirst(); // remove the first node
      void insertLast(int newobj); // insert to the last of the list
      int removeLast(); // remove the last node
  
  };
  
  // output operator
  ostream&amp; operator&lt;&lt;(ostream&amp; out, const DoublyLinkedList&amp; dll);
  </code></pre>
  
  <p>DoublyLinkedList.cpp - Copy Constructor</p>
  
  <pre><code>// copy constructor
  DoublyLinkedList::DoublyLinkedList(const DoublyLinkedList&amp; dll)
  {
    // Initialize the list
    header = 0;
    trailer = 0;
    header.next = &amp;trailer; 
    trailer.prev = &amp;header;
  
    // Copy from dll
      DListNode* head = new DListNode;
      head-&gt;prev = dll.header.prev;
      head-&gt;obj = dll.header.obj;
      head-&gt;next = dll.header.next;
  
      DListNode* tail = new DListNode;
      tail-&gt;prev = dll.trailer.prev;
      tail-&gt;obj = dll.trailer.obj;
      tail-&gt;next = dll.trailer.next;
  
      DListNode* curr = new DListNode;
      curr-&gt;prev = head;
  
      while(curr != tail) {
  
          DListNode* n = new DListNode;
          curr-&gt;next = n;
          n = curr-&gt;prev-&gt;next;
          curr = curr-&gt;next;
      }
  
      curr = tail;
  }
  </code></pre>