I'm doing a project in C, but I got stuck when I wanted to order the Nodes. I have methods to fill and eliminate the nodes, the problem comes when trying to order does not do anything, it leaves it as it is. When entering a user I assigned his name and his account. The other methods if they are correct, when entering users "first" remains pointing to the first node and last, to the last one entered.
typedef struct nodo {
char nombre[60];
int cuenta;
struct nodo *siguiente;
} nodo;
nodo*primero=NULL;
nodo*ultimo=NULL;
int i=0;
void organiza(){
nodo*actual=(nodo*)malloc(sizeof(nodo));
nodo*aux=(nodo*)malloc(sizeof(nodo));
nodo*pivote=(nodo*)malloc(sizeof(nodo));
actual=primero;
pivote = primero;
if(primero!=NULL){
while(actual!=NULL){
aux=actual->siguiente;
while(aux!=NULL){
if(actual->cuenta > aux->cuenta){
pivote = aux;
aux = actual;
actual->cuenta = pivote;
}
}
aux=aux->siguiente;
}
actual=actual->siguiente;
} else {
printf("Vacia\n");
}
}
I think the problem is within the second if. But I'm not sure what. I already did
if(actual->cuenta> aux->cuenta){
pivote = aux->cuenta;
aux->cuenta = actual->cuenta;
actual->cuenta = pivote;
}
And if it works, but it does not work when I do it with nodo->nombres
, even creating a pivot for the names. Which means that it only orders the accounts but the names remain the same.