Sort list pointers doubly linked

0

There is the following structure:

typedef struct nodo{
    int matricula, edad;
    char nombre[20];
    float peso, altura;
    struct nodo *next, *prev;
} List;
List *first, *buffer, *buffer2;

And to sort it, according to the member, there are functions (this is to sort by registration):

void ordenar_matricula(void){
int aux;
char auxc[50];
float auxf;
buffer = first;
while(buffer->next != NULL){
    buffer2 = buffer->next;
    while(buffer2 != NULL){
        if(buffer->matricula > buffer2->matricula){
            aux = buffer2->matricula;
            buffer2->matricula = buffer->matricula;
            buffer->matricula = aux;

            strcpy(auxc,buffer2->nombre);
            strcpy(buffer2->nombre,buffer->nombre);
            strcpy(buffer->nombre,auxc);

            aux = buffer2->edad;
            buffer2->edad = buffer->edad;
            buffer->edad = aux;

            auxf = buffer2->peso;
            buffer2->peso = buffer->peso;
            buffer->peso = auxf;

            auxf = buffer2->altura;
            buffer2->altura = buffer->altura;
            buffer->altura = auxf;
        }
        buffer2 = buffer2->next;
    }
    buffer = buffer->next;
}
imprimir();

return;
}

As you can see, what it really does is exchange each member according to the order. It works, but what I want to know is why this does not work:

void ordenar_matricula(void){
List *aux;
buffer = first;
while(buffer->next != NULL){
    buffer2 = buffer->next;
    while(buffer2 != NULL){
        if(buffer->matricula > buffer2->matricula){
            aux = buffer2;
            buffer2 = buffer;
            buffer = aux;
        }
        buffer2 = buffer2->next;
    }
    buffer = buffer->next;
}
imprimir();

return;
}

That is, why it is not ordered when trying to exchange pointers directly. I do not think it's the method because I tried another one but the program crashed.

Thanks in advance.

    
asked by netotz 01.05.2018 в 02:12
source

1 answer

0

The problem is found in the following three lines:

aux = buffer2;
buffer2 = buffer;
buffer = aux;

The problem is that you are exchanging the pointers, but not the content of the structure pointed at by those pointers. If you want to change the content of the structures that point the pointers, you could write like this:

aux = *buffer2;
*buffer2 = *buffer;
*buffer = aux;

Where aux , is not a pointer to List, but a List structure. In other words, you would define aux as follows.

List aux;

But ... here also another problem arises, that although you really exchange the contents of the structures, you are also modifying the pointers next and prev .

One solution is "knowing" that next and prev are at the end of the structure, what you could do is use the memcpy function , copy the entire structure except the last two pointers, as follows:

void ordenar_matricula(void)
{
    List aux;
    buffer = first;
    while(buffer->next != NULL)
    {
        buffer2 = buffer->next;
        while(buffer2 != NULL)
        {
            if(buffer->matricula > buffer2->matricula)
            {
                memcpy(&aux, buffer2, sizeof(List) - sizeof(List*)*2);
                memcpy(buffer2, buffer, sizeof(List) - sizeof(List*)*2);
                memcpy(buffer, &aux, sizeof(List) - sizeof(List*)*2);
            }
            buffer2 = buffer2->next;
        }
        buffer = buffer->next;
    }
    imprimir();
}
    
answered by 24.12.2018 в 14:11