Program stops responding when I try to fill in the list

1

This is the code, it simply leverages the values and puts them in the list, but in the last loop it stops responding

#include<stdio.h>
#include<stdlib.h>
typedef struct pessoa {
    int horas,minutos;
    int conversao;
    int atendimento;
    int critico;
    int horacritica;
    int horasair;
    struct pessoa *proximo;
}paciente;

paciente *cabeca;
paciente *auxiliar;
paciente *anterior;

void insere(paciente *cabeca){
    paciente *elemento =(paciente*)malloc(sizeof(paciente));


    scanf("%d %d",&elemento->horas,&elemento->minutos);

    scanf("%d",&elemento->critico);

    if(cabeca->proximo == NULL){
        cabeca->proximo = elemento;
    } else {
        auxiliar->proximo = cabeca;
        while(auxiliar->proximo != NULL){
            auxiliar = auxiliar->proximo;
        }
        auxiliar->proximo = elemento;
    }


}

int main(){
    int N,cout;
    cabeca = (paciente*)malloc(sizeof(paciente));
    cabeca->proximo = NULL;

    printf(" QUANTOS NOS QUER : ");
    scanf("%d",&N);
    for(cout=1;cout<=N;cout++){
        insere(cabeca);
    }


    return 0;
}
    
asked by anonymous 11.11.2017 / 22:38

1 answer

0

You are not initializing the value of elemento->proximo in the function insere .

Placing elemento->proximo = NULL soon after malloc already solves your problem.

Another alternative would be to use calloc , which does a clean allocation (all bytes begin with 0).

    
11.11.2017 / 23:38