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.