Search code examples
c++bubble-sortsingly-linked-list

Sorting a Singly Linked List With Pointers


I am trying to sort a singly linked list using bubble sort by manipulating ONLY the pointers, no keys.

The following gets stuck in the for loop and loops infinitely. I don't understand why this is. Can anybody explain to me why the end of the list is not being found?

Node* sort_list(Node* head)
{
    Node * temp;
    Node * curr;
    for(bool didSwap = true; didSwap; ) {
            didSwap = false;
            for(curr = head; curr->next != NULL; curr = curr->next) {
                    if(curr->key > curr->next->key) {
                            temp = curr;
                            curr = curr->next;
                            curr->next = temp;
                            didSwap = true;
                    }
                    cout << curr->next->key << endl;
            }
    }
    return head;

}

If I change the code so that the keys (data) are swapped, then the function works properly but for some reason I am not able make it work by manipulating only pointers.


Solution

  • Logical Error, you are creating an infinite loop with following code -

    temp = curr;
    curr = curr->next;
    curr->next = temp;
    

    I,e next_of_current is pointing to current, so curr->next will always be curr and never will be NULL;

    Next you should use previous pointer to fix your list because your list can be traversed in a single direction. So, Think - If A->B->C->NULL; and you make C and B swap then the new list will still point to A->B and next iteration will be wrong ... because you are not modifying your previous next.

    So, another implementation may be -

    Node* sort_list(Node* head) {
        Node * curr;
        Node * prev;
        for(bool didSwap = true; didSwap; ) {
            didSwap = false;
            prev = head;
            for(curr = head; curr->next != NULL; curr = curr->next) {
                    if(curr->key > curr->next->key) {
                            if (head == curr) {
                                head = curr->next;      
                                curr->next = head->next; 
                                head->next = curr; 
                                prev = head;
                            } else {
                                prev->next = curr->next;
                                curr->next = prev->next->next;
                                prev->next->next = curr
                            }
                            didSwap = true;
                    } else if (head != curr) {
                        prev = prev->next;
                    } 
                    //cout << curr->next->key << endl; // <- this may cause crash if curr->next now points to NULL; (i,e last element)
                }
        }
        return head;
    }
    

    Hope this helps, regards.