title: Swap first 2 nodes only in a doubly linked list
body: <p>In the reverse function, I'm trying to swap the first 2 nodes only in a doubly linked list without swapping the data.  It runs but it's not swapping the first 2 nodes in the list.  Can someone point me in the right direction as to what I'm doing wrong?  </p>
  
  <pre><code> struct node {
      int data;
      node * p;                //  FORWARD LINK
      node * rp;               //  REVERSE LINK
   };
  
   ostream &amp; operator&lt;&lt;(ostream &amp;, const node *);  
   void addFront(node * &amp; start, int);             
   void cleanUp(node *);                          
   void reverse(node * &amp;);              
  
   void main()
   {
  
      node * a = NULL;
      cout &lt;&lt; "EMPTY LIST CASE:\n";
      cout &lt;&lt; "BEFORE a is\n" &lt;&lt; a &lt;&lt; endl;
      reverse(a);
      cout &lt;&lt; "AFTER a is\n" &lt;&lt; a &lt;&lt; endl;
  
      cleanUp(a);
      a = NULL;
      addFront(a, 10);
      cout &lt;&lt; "\nONE ELEMENT LIST CASE:\n";
      cout &lt;&lt; "BEFORE a is\n" &lt;&lt; a &lt;&lt; endl;
      reverse(a);
      cout &lt;&lt; "AFTER a is\n" &lt;&lt; a &lt;&lt; endl;
  
      cleanUp(a);
      a = NULL;
      addFront(a, 30);
      addFront(a, 20);
      cout &lt;&lt; "\nTWO ELEMENT LIST CASE:\n";
      cout &lt;&lt; "BEFORE a is\n" &lt;&lt; a &lt;&lt; endl;
      reverse(a);
      cout &lt;&lt; "AFTER a is\n" &lt;&lt; a &lt;&lt; endl;
  
      cleanUp(a);
      a = NULL;
      addFront(a, 60);
      addFront(a, 50);
      addFront(a, 40);
      cout &lt;&lt; "\nTHREE ELEMENT LIST CASE:\n";
      cout &lt;&lt; "BEFORE a is\n" &lt;&lt; a &lt;&lt; endl;
      reverse(a);
      cout &lt;&lt; "AFTER a is\n" &lt;&lt; a &lt;&lt; endl;
  
      cleanUp(a);
      a = NULL;
      addFront(a, 400);
      addFront(a, 300);
      addFront(a, 200);
      addFront(a, 100);
      cout &lt;&lt; "\nFOUR ELEMENT LIST CASE:\n";
      cout &lt;&lt; "BEFORE a is\n" &lt;&lt; a &lt;&lt; endl;
      reverse(a);
      cout &lt;&lt; "AFTER a is\n" &lt;&lt; a &lt;&lt; endl;
  
      cleanUp(a);
   }
  
  
   void reverse(node * &amp; s)   
   {
      node * n1 = s;
      node * n2 = s;
  
      if (n1 == NULL) return;
  
      node * temp = new node;
  
      temp-&gt;rp = n1-&gt;rp;
      temp-&gt;p = n1-&gt;p;
  
      n1-&gt;rp = n2-&gt;rp;
      n1-&gt;p = n2-&gt;p;
  
      n2-&gt;rp = temp-&gt;rp;
      n2-&gt;p = temp-&gt;p;
  
      if (n1-&gt;p != NULL)
           n1-&gt;p-&gt;rp = n1;
      if (n1-&gt;rp != NULL)
           n1-&gt;rp-&gt;p = n1;
      if (n2-&gt;p != NULL)
           n2-&gt;p-&gt;rp = n2;
      if (n2-&gt;rp != NULL)
           n2-&gt;rp-&gt;p = n2;
  
      delete temp;
   }
  
  
   void addFront(node * &amp; start, int x)
   {
      node * t = new node;
      t-&gt;data = x;
      if (start != NULL)
      {
          t-&gt;p = start;
           t-&gt;rp = NULL;
          start-&gt;rp = t;
      }
      else
      {
           t-&gt;p = NULL;
           t-&gt;rp = NULL;
      }
      start = t;
   }
  
   void cleanUp(node * s)
   {
      node * walker, *prev;
      walker = s;
      while (walker != NULL)
      {
          prev = walker;
           walker = walker-&gt;p;
          delete prev;
      }
   }    
  
   ostream &amp; operator&lt;&lt;(ostream &amp; w, const node * s)
   {
      const node * walker = s;
      const node * trailer = walker;
      w &lt;&lt; "Forward Print " &lt;&lt; endl;
      if (s == NULL)
      {
          w &lt;&lt; "EMPTY LIST";
      }
      else
      {
          while (walker != NULL)
          {
              w &lt;&lt; walker-&gt;data &lt;&lt; ' ';
              trailer = walker;
              walker = walker-&gt;p;
          }
      }
  
      w &lt;&lt; endl &lt;&lt; "Reverse Print " &lt;&lt; endl;
      if (trailer == NULL)
      {
          w &lt;&lt; "EMPTY LIST";
           return w;
      }
  
      while (trailer != NULL)
      {
           w &lt;&lt; trailer-&gt;data &lt;&lt; ' ';
          trailer = trailer-&gt;rp;
      }
      return w;
   }
  </code></pre>
  
  <p>Updated with complete program.</p>