Search code examples
algorithmdata-structureslinked-listcircular-list

Sorting singly circular linked list


I'm trying to sort the singly circular linked list after each edit. But my code doesn't work. I based it on the selection sort algorithm. I've been doing this for hours but can't seem to get the correct code.

void editList(node *head, int value, int newValue)
{
    node *traverser = head;
    do {
        traverser = traverser -> next;
    }while(traverser -> data != value);

    traverser -> data = newValue;

    node *index;
    node *selection;
    node *temp = new node;

    for(index = head; index -> next != head; index = index -> next) {

        for(selection = head; selection -> next != head; selection = selection -> next) {
            if(index -> data > selection -> data) {
                temp -> data = index-> data;
                index -> data = selection -> data;
                selection -> data = temp -> data;

            }
        }//End of outer loop

    }//End of sorting

    return;
}//End of editList()

Solution

  • When analyzing the provided source code, the proposed sort algorithm is very close to the expected 'selection sort algorithm'.

    The two nested loop are present, but doing independent features.

    Step 1 - performing a true nested loop by including condition from the first into the second loop.

    To sort all the list, the selection starts from the next node of the index.

    for(index = head; index -> next != head; index = index -> next) {
        // start the selection from the index->next
        for(selection = index->next; selection -> next != head; selection = selection -> next) {
            if(index -> data > selection -> data) {
                temp -> data = index-> data;
                index -> data = selection -> data;
                selection -> data = temp -> data;
    
            }
        }//End of outer loop
    }//End of sorting
    

    Bonus 1 - because the swap is only performed at the data level, instead of using a temporary node, just use a integer.

    // use just an integer
    int temp;
    ...
    temp = index-> data;
    index -> data = selection -> data;
    selection -> data = temp;
    

    Instead of:

    // allocated but never freed
    node *temp = new node;
    ...
    temp -> data = index-> data;
    index -> data = selection -> data;
    selection -> data = temp -> data;
    

    Bonus 2 - for the first search part to localize the node having the int value to be replaced, why not using the same loop structure to prevent a never-ending loop if no node has the searched value.

    node *traverser;
    // a ending loop to search a value into a circular-list
    for(traverser = head; traverser->next != head; traverser = traverser -> next) {
        if (traverser -> data == value) {
            traverser -> data = newValue;
            break;
        }
    }
    

    Instead of

    node *traverser = head;
    do {
        traverser = traverser -> next;
    }while(traverser -> data != value);
    
    traverser -> data = newValue;