title: Bubble sort singly linked list in C with pointers
body: <p>I am trying to bubble-sort a singly linked list using pointer manipulation in C. I've looked at some other implementations of bubble-sort on the website, but I feel like the logic of my code here should make sense. Even so, still entering into an infinite loop. Any help would be greatly appreciated! </p>
  
  <pre><code>int counter;
  struct node* current = head;
  struct node* previous = (struct node*) malloc(sizeof(struct node));
  struct node* next = (struct node*) malloc(sizeof(struct node));
  
  for (counter = 0; counter &lt; num_nodes; counter++){
      current = head;
      next = current-&gt;m_next;
  
      while(next != NULL){
          int compare = strcmp(current-&gt;m_last_name, next-&gt;m_last_name);
          if (compare &gt; 0){
              if (current == head){
                  head = next;
              }
              previous-&gt;m_next = next;
              current-&gt;m_next = next-&gt;m_next;
              next-&gt;m_next = current; 
  
              previous = next;
              next = current-&gt;m_next;
          }
          else {
              previous = current;
              current = current-&gt;m_next;
              next = current-&gt;m_next;
          }
      }
  }
  printf("Loop completely done\n");
  </code></pre>
  
  <p>}</p>