Organize chained list

Asked

Viewed 42 times

0

Hello I’m trying to organize a simple chained list but I’m losing the nodes reference, if you can give me a light... For example, if the previous node has the attribute "->date" larger than the later one, I should change them...

void organizarMenorMaior(node **ptrAux){ // recebe a cabeça do nó como parâmetro, ordena lista do menor para o maior 
    system("cls");
    node *p, *p2;
    p = *ptrAux;
    if(p==NULL){ // Verifica se a lista está vazia
        printf("Lista vazia!");
    }else{
        if(p->next==NULL){ // Verifica se a lista tem mais de 1 elemento
            printf("A lista tem apenas 1 elemento!");
        }else{
            p2 = p->next;
            if(p->data>p2->data){ // CORRIGIR AQUI, CONDIÇÃO NECESSÁRIA PAR FAZER A TROCA DE NÓS
                p->next = p2->next;
                p2->next = p;
            }else{
                printf("A lista ja esta organizada.\n");
            }
        }
    }
};
  • 1

    What you’re trying to do is sort the list ? If it’s sort, you can’t just sort it with if and without loops

  • I would like one example only, I am aware that it is not possible to do without loop...

  • But the goal is to sort ? It is not clear to me. If this is the case edit the question and make it clear that you want to sort a chained list

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.