Segmentation fault, Stack in c

Asked

Viewed 73 times

2

I don’t know what might be giving this error in my code, please if anyone knows what is 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");
    }
}

1 answer

2


There are some small errors in the code:

  • The comparison in fila_Vazia is made with only one = thus:

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

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

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

  • Still in office insere_Fila is not modifying the tail appropriately

  • The printf in the imprime_Fila is %c when the dado is the 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 code working on Ideone

Suggestion

Since you are using tam for the size and tail for the tail of the list it would be good idea to use another structure representing the list, with these 3 fields.

Example:

struct lista {
    no_pont head,
    no_pont tail,
    size_t tam
};

Browser other questions tagged

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