title: Copy constructor of a doubly linked list (c++)
body: <p>I have been having trouble implementing a copy constructor for a doubly linked list. My professor provided a skeleton for a DLL class and i am implementing the functions as part of a project. Everything i have seen online has not proven to be too helpful because everything i see looks entirely different to the format of code i am supposed to be using.</p>
  
  <p>For starters here's the DLL.h file</p>
  
  <pre><code>#include &lt;string&gt;
  using namespace std;
  
  struct Node{
      string ssn;
      string name;
      Node* succ;
      Node* pred;
  };
  
  class DLL{
      private:
          Node* headPtr;
          int itemCount;
  
      public:
          DLL();
          DLL(DLL&amp; n); //Copy constructor
          virtual ~DLL();
          Node* getHeadPtr();
          int search(string ss) const;
          bool insert(string ss, string name, int &amp; count);
          bool remove(string ss, int &amp; count);
          int size();
          void display();
  };
  </code></pre>
  
  <p>I have all the other functions implemented besides the destructor, which i haven't worked on yet but so far this is my code for the copy constructor: </p>
  
  <pre><code>DLL::DLL(DLL&amp; n){
      n.headPtr = nullptr;
      n.itemCount = 0;
  
      Node* temp = headPtr;
      while(temp!= NULL){
          temp = temp-&gt;succ;
          insert(temp-&gt;ssn, temp-&gt;name, n.itemCount);
      }   
  }
  </code></pre>
  
  <p>When i run this i keep getting segfault's. I have searched for some time on the internet but nothing similar enough to this format for me to comprehend has come up.</p>
  
  <p>All help/suggestions/advice would be greatly appreciated thanks!</p>
  
  <p>Edit: So now i have this, which works technically but a requirement is that the memory addresses are different. I was provided with a file test.cpp that runs the functions with example parameters and in the end the run is like Orig list: 12, 13, 14 (with memory adresses) followed by the new copied one.</p>
  
  <p>Before i was getting segfault 11 but now it runs but is at the same memory addresses.</p>
  
  <pre><code>DLL::DLL(DLL&amp; n){
  
  Node* temp = n.headPtr;
  headPtr = temp;
  int count = 0;
  
  while(temp != NULL){
      insert(temp-&gt;ssn, temp-&gt;name, count);
      temp = temp-&gt;succ;
  }   
  </code></pre>
  
  <p>}</p>