passage by struct reference does not work (Dynamic queue C)

Asked

Viewed 83 times

-1

I’m trying to implement a dynamic Fila. When I start the program (start and end = NULL), and then place to queue a value (queue function) the pointer "end" receives the new queue item, but the "start" does not, and in the function I am asking for the "start" and the "end" to receive the new queue item.

Follow the code for testing:

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

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

void enfileirar(struct no **inicio, struct no *fim, int valor) {
    struct no *novoNo;

    novoNo = (struct no *) malloc(sizeof(struct no));

    novoNo->dado = valor;
    novoNo->prox = NULL;

    if (*inicio == NULL) {
        *inicio = novoNo;
    } else {
        *fim->prox = *novoNo;
    }

    *fim = *novoNo; 
}

void mostrarFila(struct no **inicio) {
    struct no *aux;
    aux = inicio;

    printf("Inicio da fila -> \n");
    while(aux == NULL) {
        printf("%d\n", &aux->dado);
        printf("\n");
        aux = aux->prox;
    }
    printf("<- Fim da fila \n");
}

int main(int argc, char** argv) {
    struct no *inicio, *fim;
    int resposta, valor;
    bool sair = true;

    while(sair) {
        printf("\n");
        printf("************** MENU **************\n");
        printf("0 - Sair do programa\n");
        printf("1 - Iniciar Fila\n");
        printf("2 - Enfileirar\n");
        printf("3 - Mostrar fila\n");
        printf("**********************************\n");

        scanf("%d", &resposta);

        switch(resposta) {
            case 0:
                sair = false;
                printf("Saindo do programa...\n");
                break;
            case 1:
                inicio = NULL;
                fim = NULL;
                break;
            case 2:
                printf("digite o valor para inserir na fila:\n");
                scanf("%d", &valor);
                enfileirar(&inicio, &fim, valor);
                printf("%d\n", &inicio->dado);
                printf("%d\n", &fim->dado);
                break;
            case 3:
                mostrarFila(&inicio);
                break;
            default:
                sair = false;
                printf("Saindo do programa...\n");
                break;
        }
    }

    return 0;
}

1 answer

0


There are several problems in the code.

  1. In function enfileirar() the parameter fim should be a pointer to pointer:

    void enfileirar(struct no **inicio, struct no **fim, int valor) {

  2. And that forces the pointer fim in brackets in the assignment:

    (*fim)->prox = novoNo;

  3. In function mostrarFila() there is no need to pass pointer to pointer since there will be no change in the list

    void mostrarFila(struct no *inicio) {

  4. And assignment to aux is simple:

    aux = inicio;

  5. The test was wrong:

    while(aux != NULL) {

  6. And you want to print the data and not his address:

    printf("%d\n", aux->dado);

  7. Same thing in the job main()

    printf("%d\n", inicio->dado); printf("%d\n", fim->dado);

  8. And call to mostrarFila() mute to:

    mostrarFila(inicio);

Several of these problems were detected as warnings in the compilation.

Browser other questions tagged

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