Friday 21 April 2017

How to swap nodes in a linked list without swapping data in C language?

I use this sorting function, but I don't know how to swap node addresses instead of values.

Thank you

void sort(LISTnode **h) {
    LISTnode * start, * min, * temp;
    int num;
    start = *h;
    while (start->next != NULL) {
        temp = start;
        min = temp;
        temp = temp->next;

        while (temp != NULL) {
            if (temp->number < min->number)
                min = temp;
                temp = temp->next;
        }

        // This part of the code
        num = min->number;
        min->number = start->number;
        start->number = num;

        start = start->next;
    }    
}



via Peter Povy

No comments:

Post a Comment