Segmentation fault, stack in c

2

I do not know what might be giving this error in my code, please if anyone knows what's happening. Here's the code.

#include <stdio.h>
#include <stdlib.h>

struct no {
    int dado;
    struct no *prox;
};

typedef struct no criano;
typedef criano *no_pont;
int tam;

//void cria_Fila(no_pont *head, no_pont *tail);
int fila_Vazia(no_pont head);
void insere_Fila(no_pont *head, no_pont *tail, int valor);
int retira_Fila(no_pont *head, no_pont *tail);
void imprime_Fila(no_pont atual);

int main() {
    no_pont head=NULL;
    no_pont tail=NULL;
//  cria_Fila(&head,&tail);
    insere_Fila(&head, &tail, 52);
    insere_Fila(&head, &tail, 21);
    insere_Fila(&head, &tail, 532);
    imprime_Fila(head);

    return 0;
}

/*void cria_Fila(no_pont *head, no_pont *tail, int tam){
head=NULL;
tail=NULL;
tam=0;
}*/

int fila_Vazia(no_pont head) {
    return head=NULL;
}

void insere_Fila(no_pont *head, no_pont *tail, int valor) {
    no_pont novo;
    novo = (no_pont*)malloc(sizeof(no_pont));

    if(novo!=NULL) {
        novo->dado=valor;
        novo->prox=NULL;

        if(fila_Vazia(*head)) {
            *head=novo;
        } else {
            (*tail)->prox=novo;
        }
    } else {
        printf("%d nao inserido. Nao ha espaco na memoria\n",valor);
    }
    tam++;
}

int retira_Fila(no_pont *head, no_pont *tail) {
    int valor;
    no_pont aux;

    valor = (*head)->dado;
    aux= *head;
    *head= (*head)->prox;
    if( *head == NULL ) {
        *tail = NULL;
    }
    free(aux);
    tam--;
    return valor;
}

void imprime_Fila(no_pont atual) {
    if(atual == NULL) {
        printf("A fila esta vazia\n");
    } else {
        printf("A fila e: ");

        while(atual!=NULL) {
            printf("%c  ",atual->dado);
            atual=atual->prox;
        }

        printf("NULL\n\n");
    }
}
    
asked by anonymous 28.09.2017 / 14:33

1 answer

2

You have some small errors in the code:

  • The comparison in fila_Vazia is done only with = like this:

    return head=NULL;
    
  • In function insere_Fila the allocation of the new node is not correct:

    novo = (no_pont*)malloc(sizeof(no_pont));
    

    Because it allocates a pointer to the node instead of the node itself.

  • Still in function insere_Fila is not modifying tail properly

  • The printf in imprime_Fila is %c when dado is of type int .

The function fila_Vazia and insere_Fila should look like this:

int fila_Vazia(no_pont head) {
    return head==NULL; //agora com ==
}

void insere_Fila(no_pont *head, no_pont *tail, int valor) {
    no_pont novo;
    novo = (no_pont)malloc(sizeof(criano)); //alocação de criano em vez de no_pont

    if(novo!=NULL) {
        novo->dado=valor;
        novo->prox=NULL;

        if(fila_Vazia(*head)) {
            *head=novo;
        } else {
            (*tail)->prox=novo;
        }

        *tail = novo; //modificação do tail para o ultimo nó, que é o novo

    } else {
        printf("%d nao inserido. Nao ha espaco na memoria\n",valor);
    }
    tam++;
}

See the code working on Ideone

Suggestion

Since you are using tam for size and tail for the tail of the list it would be a good idea to use another structure that represents the list with those 3 fields.

Example:

struct lista {
    no_pont head,
    no_pont tail,
    size_t tam
};
    
28.09.2017 / 14:58