title: How to Sort a List with Bubble sort in C++
body: <p>I have this class:</p>
  
  <pre><code>class Elem
  {
  public:
      int x;
      Elem *nast;
  };
  </code></pre>
  
  <p>I have a default constructor, function to show <code>x</code>. I made ten element list, but how to sort this list ordered by <code>x</code>?</p>
  
  <p>I tried this:</p>
  
  <pre><code>void Sortlinked_list(Elem *head)
  {
      int ile = 0;
      Elem *cur;
      cur = head;
      while( cur-&gt;nast != NULL )
      {
          cur = cur-&gt;nast;
          ile++;
      }
  
      Elem* curr = head;
      Elem* next;
      int temp;
  
      for(int i = 0; i &lt; ile; i++ )
      {
          while( curr &amp;&amp; curr-&gt;nast )
          {
  
              next = curr-&gt;nast;
              while (next)
              {
                  if (curr-&gt;show() &gt; next-&gt;show())
                  {
                      std::swap(next-&gt;nast, curr-&gt;nast);
                  }
                  next = next-&gt;nast;
              }
              curr = curr-&gt;nast;
          }
      }
  }
  </code></pre>
  
  <p>But it doesn't work. The output is: <a href="http://i.stack.imgur.com/vJrRK.png" rel="nofollow">http://i.stack.imgur.com/vJrRK.png</a></p>
  
  <p>If any one can help me with this? I spent 3 hours and made nothing.</p>