C
C
ccc352015-12-11 20:04:23
C++ / C#
ccc35, 2015-12-11 20:04:23

How to reset pointers between nodes in a singly linked list to swap elements?

It is necessary to swap elements for selection sorting. There is the following function

void Sort()
{
    node *h = HEAD, *i, *j, *next_i;
    for(i = h; i!=NULL && i->NEXT!=NULL; i=i->NEXT)
    {
        node *min;
        min = i;
        for(j = i->NEXT; j!=NULL ; j=j->NEXT)
        {
            if(j->DATA < min->DATA)
                min=j;
        }
        if(min!=i)
        {
            int temp;
            temp = min->DATA;
            min->DATA = i->DATA;
            i->DATA = temp;
        }
    }
    HEAD = h;
}

The function works, but instead of the pointers, the values ​​stored in the nodes change. As far as I understand, this is contrary to the principles of working with lists, because there, the main idea is to swap pointers, not the value of the list node itself.
Actually a question how to make it in this case?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question