1
I’m trying to create a function that traverses a binary tree in levels through a chained queue, but when I compile it shows the error message "Segmentation failure (recorded core image)".
//Estrutura da árvore
typedef struct no{
int chave;
struct no* esq;
struct no* dir;
}NO;
typedef struct arvore{
NO* pont;
}ARVORE;
//Estrutura da fila
typedef struct elemento{
NO* reg;
struct elemento* prox;
}ELEMENTO;
typedef struct fila{
ELEMENTO* head;
}FILA;
void criaFila(FILA* fila){
fila->head = NULL;
}
void insereFila(FILA* fila, NO* p){
ELEMENTO* novo = (ELEMENTO *) malloc(sizeof(ELEMENTO));
novo->reg = p;
novo->prox = NULL;
if(fila->head==NULL){
fila->head = novo;
}else{
ELEMENTO* pointer = fila->head;
ELEMENTO* ant = NULL;
while(pointer!=NULL){
ant = pointer;
pointer = pointer->prox;
}
ant->prox = novo;
free(pointer);
}
}
NO* removeFila(FILA* fila){
if(fila->head!=NULL){
ELEMENTO* pointer = fila->head;
fila->head = pointer->prox;
return pointer->reg;
}
}
int tamanhoFila(FILA* fila){
ELEMENTO* pointer = fila->head;
int tam;
while(pointer!=NULL){
tam++;
pointer = pointer->prox;
}
return tam;
}
void percurso(NO* raiz){
FILA fila;
NO* prox;
criaFila(&fila);
insereFila(&fila, raiz);
while(tamanhoFila(&fila)>0){
prox = removeFila(&fila);
printf("%d ", prox->chave);
if(prox->esq!=NULL){
insereFila(&fila, prox->esq);
}
if(prox->dir!=NULL){
insereFila(&fila, prox->dir);
}
}
}
"segmentation failure" occurs in program execution, not in compilation!
– zentrunix