Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Im working on a project for school in which I need to sort a linked list with any sorting algorithm (preferably bubble) - please note that swapping each node data is not allowd as I am expected to swap the nodes. I can not paste all the code of my project as it is very wide and it is in Spanish, reason for which I will show you the parts in which I try to implement the node swapping.

To sum up, the project itself is quite simple, a linked list where each node contains information of a person (name, last name, age, etc).

I have to sort the list alphabetically. Please, bear in mind that I cannot implement the sorting nor the swapping algorithms in functions (That is to say, I cannot make a 'swapNodes' function and them implement it in the code.

I managed to swap two nodes following this logic:

firstNode = auxiliar->next;
auxiliar-> next = firstNode->next;
firstNode->next = auxiliar;

This works to swap 2 nodes perfectly.

Problem is when I have to implement this logic with a sorting algorithm in order for it to sort all the list, and this is where I need you guys to help me.

I have checked every post about bubblesorting and node swapping but I just can get it working.

My professor tried to help me, implementing my node swapping code in the following way (please note that as an example, the following code should sorts by age):

while (flag == 1) {
        auxiliar = firstNode;
        flag = 0;

        if (auxiliar->edad > auxiliarSiguiente->edad) {

              firstNode = auxiliar->next;
              auxiliar-> next = firstNode->next;
              firstNode->next = auxiliar;
              flag = 1;

        }

        auxiliar3 = firstNode;

        while (auxiliar->next != NULL ) {

            if (auxiliar->age > auxiliarNext->age) {

                auxiliar2 = auxiliar->next;
                auxiliar->next = auxiliar2->next;
                auxiliar2->next = auxiliar;
                auxiliar3->next = auxiliar2;

            } else {
                auxiliar = auxiliar->next;
            }

            auxiliar3 = auxiliar3->next;

        }

    }

And then again, this code works just fine when there are 2 nodes. In case there are more, it does not sort in a correct way. Moreover, Im not quite sure which sorting algorithm my professor used.

Perhaps what my teacher did is wrong, or maybe there is a simpler way to do it.

I greatly appreciate your feedback, as I could not manage to find a way to solve it myself.

Hope you guys can help me!

share|improve this question
    
you should consider having a doubly linked list, if not you can´t swap/insert/remove current node, just node after –  sp2danny 15 hours ago
    
I am not allowed to use a doubly linked list –  user3895347 11 hours ago

1 Answer 1

Your original swap code does not alter the next pointer of the node that pointed to the node auxiliar pointed to before the swap (which there will be if auxiliar isn't the first node in the list).

share|improve this answer
    
Thank you for your reply, Scott! If I did not do anything wrong, I alter the next pointer of the node previous to auxiliar (considering it is not the first node) at the last part of the code (forgot to indicate that is the reason for which auxiliar 2 and 3 exist, both of which are initialized pointing to the first node) –  user3895347 10 hours ago
    
If you can, verify that your swap code works; if it doesn't, there isn't much point in going further. (It would be easier if it were its own function; you could write it that way during development, then "unroll" it for final submission.) Once that is done, then you can focus on how it is used in your bubble sort. –  Scott Hunter 8 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.