How to create simple threaded list circular in C?

0

NOTE: The first element I am inserting out of the chi, this is the second on, the while will run until the user types the id of a student -1 q ta in the create function. It's falling into an infinite loop!

void inserir(Aluno *aluno){

while(1){
    if(aluno->prox == NULL){
        Aluno *aux = aluno;
        Aluno *novo = criar();
        if(novo->id < 0)
            break;
        aux->prox = novo;
        novo->prox = aux;
    }else{

        Aluno *aux = aluno->prox;
        while(aux->prox != aluno){ // entra e um loop infinito
            printf("3\n");
            aux = aux->prox;

        }
        Aluno *novo = criar();
            if(novo->id < 0)
                break;  
            aux->prox=novo;
            novo->prox=aux; 
    }
}
}
    
asked by anonymous 20.05.2018 / 21:14

1 answer

0

The simple circular list is defined by instead of the next last one to null, it points to the first one in the list.

       Aluno *novo = criar();
        if(novo->id < 0)
            break;  
        aux->prox=novo;
        novo->prox=aux; 

You are creating a new node, making it point to its previous node. No knots will point to student.

    Aluno *novo = criar();
        if(novo->id < 0)
            break;  
        aux->prox=novo;
        novo->prox=aluno; 

Now, point to student and it will not fall into a loop.

    
20.05.2018 / 22:30