title: Linked list and (alphabetic) bubble sorting
body: <p>I have got a problem with bubble sorting with a linked list. I have not found the bug in my simple code for few hours. Could you look at that?</p>
  
  <pre><code>int listSort(node_t ** _list)
  {
       int size = listSize(_list);
  
       for(int i = 0; i &lt; size; i++)
       {
            node_t *pointer = *_list;
  
            for(int j = 0 ; j &lt; size - 1; j++)
            {
                 if(strcmp(pointer-&gt;title, pointer-&gt;next-&gt;title) &gt; 0)
                      listSwapWithNext(_list, j);
  
                 pointer = pointer-&gt;next;
            }
       }
  
       return 0;
  }
  </code></pre>
  
  <p>and here's swap function (but it seems to work fine, because I tested manually all boundary cases):</p>
  
  <pre><code>int listSwapWithNext(node_t **_list, size_t first)
  {
       node_t *pointer = *_list;
       node_t *ptr_b;
       node_t *ptr_c;
       node_t *ptr_d;
  
       if(!first)
            ptr_b = pointer;
  
       for(size_t i = 0; pointer-&gt;next-&gt;next; i++, pointer = pointer-&gt;next)
       {
            if(i == first - 1 || first == 0)
            {
                 if(first)
                      ptr_b = pointer-&gt;next;
  
                 ptr_c = ptr_b-&gt;next;
                 ptr_d = ptr_c-&gt;next;
  
                 if(first)
                      pointer-&gt;next = ptr_c;
                 else
                      *_list = ptr_c;
  
                 ptr_c-&gt;next = ptr_b;
                 ptr_b-&gt;next = ptr_d;
  
                 break;
            }
       }
  
       return 0;     
  }
  </code></pre>
  
  <p>It causes this crash (on line 229):
  <img src="https://i.stack.imgur.com/bZ4XK.png" alt="It causes this crash (on line 229)"></p>
  
  <p>When I modified the condidition of inner loop in sort function to:</p>
  
  <pre><code>for(int j = 0; j &lt; size - 1 &amp;&amp; pointer-&gt;next; j++)
  </code></pre>
  
  <p>to prevent reading from bad address, I noticed strange thing. Inner loop somtimes run exactly one time less than it should.</p>
  
  <p>Thank you!</p>
  
  <p><strong>EDIT:</strong>
  Here's modified version of sort function with prevent condition in inner loop and my debug indicators (made with printf()):</p>
  
  <pre><code>int listSort(node_t ** _list)
  {
       int size = listSize(_list);
       int i;
       for(i = 0; i &lt; size; i++)
       {
            node_t *pointer = *_list;
            int j;
            for(j = 0 ; j &lt; size - 1 &amp;&amp; pointer-&gt;next; j++)
            {
                 if(strcmp(pointer-&gt;title, pointer-&gt;next-&gt;title) &gt; 0)
                      listSwapWithNext(_list, j);
  
                 printf("# %d %d\n", i, j);
                 pointer = pointer-&gt;next;
            }
            printf("\n");
       }
  
       return 0;
  }
  </code></pre>
  
  <p>Here's the result in console. Look, it doesn't  do every cycle so it gaves bad sorting.</p>
  
  <p><img src="https://i.stack.imgur.com/SCp3m.png" alt="enter image description here"></p>
  
  <p><strong>EDIT2:</strong>
  Here's the essention of the problem. <a href="http://pastebin.com/e5K6C1A2" rel="nofollow noreferrer">http://pastebin.com/e5K6C1A2</a></p>
  
  <p>Still cant resolve this issue.</p>