title: Creating a copy constructor for a linked list
body: <p><strong>This is homework</strong></p>
  
  <p>I'm working on implementing a linked list class for my C++ class, and the copy constructor has be very confusing for me.</p>
  
  <p>The linked list is comprised of structs called Elems:</p>
  
  <pre><code>struct Elem 
      {
          int pri;
          data info;
          Elem * next;
      };
      Elem * head;
  </code></pre>
  
  <p>info is a separate, custom class that is stored in the Elem.</p>
  
  <p>the signature for the copy constructor is:</p>
  
  <pre><code>linkedList::linkedList( const linkedList &amp;v )
  </code></pre>
  
  <p>The issue I am having is mostly taking my logic and actually writing it as code.</p>
  
  <p>My general idea is to:</p>
  
  <ol>
  <li>Set head to v.head (head = v.head)</li>
  <li>Set the Elem's values to v's (pri = v.pri , info = v.info , next = v.next)</li>
  <li>Iterate through, repeating step 2.</li>
  </ol>
  
  <p>Is this the general idea?</p>
  
  <p>Any help would be great. <strong><em>Remember, this is homework, so no direct answers please!</em></strong></p>
  
  <p>Thank you for your time</p>
  
  <p>====================================================================================================================================================================</p>
  
  <p>Thanks for your time everybody!</p>
  
  <p>I think I have it figured out:</p>
  
  <pre><code>//Copy Constructor
  LinkedList::LinkedList( const LinkedList &amp;v )
  {
  Elem * p1 = 0;//current
  Elem * p2 = 0;//next
  
  if( v.head == 0 )
      head = 0;
  
  else
  {
      head = new Elem;
      head -&gt; pri = v.head -&gt; pri;
      head -&gt; info = v.head -&gt; info;
  
      p1 = head;
      p2 = v.head -&gt; next;
  }
  
  while( p2 )
  {
      p1 -&gt; next = new Elem;
      p1 = p1 -&gt; next;
      p1 -&gt; pri = p2 -&gt; pri;
      p1 -&gt; info = p2 -&gt; info;
  
      p2 = p2 -&gt; next;
  }
  p1 -&gt; next = 0;
  }
  </code></pre>
  
  <p>I'm pretty sure that works. I drew some logical pictures to help, and I didn't run into any issues.</p>