title: Sorting linked list with bubble sort in c++
body: <p>I have been trying to bubble sort a doubly linked list using swap function. My question is does the swap function swap the pointer, and not just the data? My code shows me it only swap the data but not the pointers. Is there any way to efficiently to swap the pointers on the linked list? Please show me the code as I am very inexperienced in coding and I do not understand other codes in other answer. </p>
  
  <pre><code>void sortPoly(PolyNode* a)
  {
    PolyNode* head =a;
    PolyNode* current = head;
    PolyNode* current_next = current-&gt;next;
  
  
    int len =Polylength(current);
  
    if(len ==1 || len ==0)
     {
        return;
     }
  
  for(int i =0; i &lt; len; i++)
  {
      for (int j =0; j&lt; len -i; j++)
      {
          int sum = current-&gt;expx + current-&gt;expy;
          cout &lt;&lt; "sum=" &lt;&lt; sum &lt;&lt; endl;
          int next_sum = current_next-&gt;expx + current_next-&gt;expy;
           cout &lt;&lt; "\t nextsum=" &lt;&lt; next_sum &lt;&lt; endl;
  
          if( sum &lt; next_sum)
          {
              cout &lt;&lt; "current=" &lt;&lt; current-&gt;coef &lt;&lt; "expx = " &lt;&lt; current-&gt;expx &lt;&lt; "expy=" &lt;&lt; current-&gt;expy &lt;&lt; endl;
              cout &lt;&lt; "current_next=" &lt;&lt; current_next-&gt;coef &lt;&lt; "expx = " &lt;&lt; current_next-&gt;expx &lt;&lt; "expy=" &lt;&lt; current_next-&gt;expy &lt;&lt; endl;
  
              std:: swap(current, current_next);
              cout &lt;&lt; endl;
              cout &lt;&lt; "swapped" &lt;&lt; endl;
  
              cout &lt;&lt; "current=" &lt;&lt; current-&gt;coef &lt;&lt; "expx = " &lt;&lt;    current-&gt;expx &lt;&lt; "expy=" &lt;&lt; current-&gt;expy &lt;&lt; endl;
              cout &lt;&lt; "current_next=" &lt;&lt; current_next-&gt;coef &lt;&lt; "expx = " &lt;&lt; current_next-&gt;expx &lt;&lt; "expy=" &lt;&lt; current_next-&gt;expy &lt;&lt; endl;
  
             cout &lt;&lt; "current=" &lt;&lt; current-&gt;coef &lt;&lt; "expx = " &lt;&lt; current-&gt;expx &lt;&lt; "expy=" &lt;&lt; current-&gt;expy &lt;&lt; endl;
             cout &lt;&lt; "current_next=" &lt;&lt; current_next-&gt;coef &lt;&lt; "expx = " &lt;&lt; current_next-&gt;expx &lt;&lt; "expy=" &lt;&lt; current_next-&gt;expy &lt;&lt; endl;
  
          current = current-&gt;next;
  
          current_next = current-&gt;next-&gt;next;
          cout &lt;&lt; "current=" &lt;&lt; current-&gt;coef &lt;&lt; "expx = " &lt;&lt; current-&gt;expx &lt;&lt; "expy=" &lt;&lt; current-&gt;expy &lt;&lt; endl;
          cout &lt;&lt; "current_next=" &lt;&lt; current_next-&gt;coef &lt;&lt; "expx = " &lt;&lt; current_next-&gt;expx &lt;&lt; "expy=" &lt;&lt; current_next-&gt;expy &lt;&lt; endl;
      }
     }
  
   }
  </code></pre>
  
  <p>This is my struct:</p>
  
  <pre><code>    struct PolyNode
      {
        int coef;
        int expx;
        int expy;
        PolyNode* prev;
        PolyNode* next;
       };
  </code></pre>
  
  <p>Please find following my response on Mac terminal.
  <a href="https://i.stack.imgur.com/pZkeL.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pZkeL.png" alt="Result for input a = xy+5-2x^6+7x^4y^2+5x^2-y^2, b= y-x+5"></a></p>