title: Reverse doubly-link list in C++
body: <p>I've been trying to figure out how to reverse the order of a doubly-linked list, but for some reason, in my function <code>void reverse()</code> runs while loop once and then crashes for some reason.  To answer some questions ahead, I'm self-teaching myself with my brothers help.  This isn't all of the code, but I have a <code>display()</code> function which prints all nodes chronologically from <code>start_ptr</code> and a switch which activates certain functions like</p>
  
  <pre><code>    case 1 : add_end(); break;
      case 2 : add_begin(); break;
      case 3 : add_index(); break;
      case 4 : del_end(); break;
      case 5 : del_begin(); break;
      case 6 : reverse(); break;
  </code></pre>
  
  <p>This is the geist of my code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  struct node
  {
      char name[20];
      char profession[20];
      int age;
      node *nxt;
      node *prv;
  };
  
  node *start_ptr = NULL;
  
  void pswap (node *pa, node *pb)
  {
      node temp = *pa;
      *pa = *pb;
      *pb = temp;
      return;
  }
  
  void reverse()
  {
      if(start_ptr==NULL)
      {
          cout &lt;&lt; "Can't do anything" &lt;&lt; endl;
      }
      else if(start_ptr-&gt;nxt==NULL)
      {
          return;
      }
      else
      {
          node *current = start_ptr;
          node *nextone = start_ptr;
          nextone=nextone-&gt;nxt-&gt;nxt;
          current=current-&gt;nxt;
          start_ptr-&gt;prv=start_ptr-&gt;nxt;
          start_ptr-&gt;nxt=NULL;
          //nextone=nextone-&gt;nxt;
          while(nextone-&gt;nxt!= NULL)
          {
              pswap(current-&gt;nxt, current-&gt;prv);
              current=nextone;
              nextone=nextone-&gt;nxt;
          }
          start_ptr=nextone;
      }
  }
  </code></pre>