queue items in a queue (pointers)

1

Hello, I'm implementing a function to insert elements in a queue
and I noticed that after the first element entered, the pointer headPtr and'tailPtr estão apontando para o mesmo lugar, logo o problema só pode estar na função lines', however I can not solve it. Could someone point out her mistake? Here is the code:

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


typedef struct node {
  int elemento;
  struct node* prox;
}FILA;

int ehvazia(FILA* p) {
  return p == NULL;
}

void verifica(FILA* p) {

    if(ehvazia(p)) {
        printf("MEMORIA INDISPONIVEL\n");
        exit(1);
    }
}

void espia(FILA* headPtr) {
    printf("%d\n", headPtr->elemento);
}

void enfileira(FILA* headPtr, FILA* tailPtr) {

    FILA* temp = malloc(sizeof(FILA));
    verifica(temp);

    int valor;
    printf("Valor: ");
    scanf("%d", &valor);

    temp->elemento = valor;

    if(ehvazia(headPtr)) {
        headPtr = temp;
        tailPtr = head;
    } else {
        tailPtr->prox = temp;
        tailPtr = temp;
    }

}

void desinfileira(FILA* headPtr, FILA* tailPtr) {

    FILA* tempPtr;

    tempPtr = headPtr;
    headPtr = headPtr->prox;

    if(ehvazia(headPtr)) {
        tailPtr = NULL;
    }

    free(tempPtr);

};



void imprimefila(FILA* headPtr, FILA* tailPtr) {

    FILA* atual;
    atual = headPtr;

    printf("A fila eh: \n");
    while(atual != NULL) {
        printf("-->%d", atual->elemento);
        atual = atual->prox;
    }

    //printf("A fila está vazia\n");


}

int main() {

    FILA* inicio = NULL;
    FILA* fim = NULL;

    int res;

    do {

        printf("DESEJA INSERIR MAIS? <1>S <0>N\n");
        scanf("%d", &res);

        if(res == 1){

            enfileira(inicio, fim);

        }

    }while(res <=  1 && res > 0);

    imprimefila(inicio, fim);

}
    
asked by anonymous 09.07.2017 / 18:52

1 answer

0

The first error appears in:

tailPtr = head; 

Within the function queue because head does not exist, it must be headPtr or temp . Then at the end of the function desinfileira has:

}tailPtr = temp;

That also can not be therefore out of function node temp does not exist since it was created inside the function. With this the compilation errors disappear, however there is another relevant detail that does not let the code work properly.

Within the function enfileira it receives the pointer to FILA , but this is a copy of the pointer that is in main, then a statement of this type:

void enfileira(FILA* headPtr, FILA* tailPtr) {
   ...
   headPtr = temp;

Does not have the desired effect because it does not change the main's pointer. A common solution to this problem is to return the new value for the pointer, but since we have two pointers it also does not work. Instead we can pass a pointer to the pointer we have in the main, calling the function like this:

int main() {
    ...
    if(res == 1){ 
        enfileira(&inicio, &fim); //passa agora o endereço do ponteiro
    }

And now in the function enfileira we have to adjust the code for this difference:

void enfileira(FILA** headPtr, FILA** tailPtr) {

    FILA* temp = malloc(sizeof(FILA));
    verifica(temp);

    int valor;
    printf("Valor: ");
    scanf("%d", &valor);

    temp->elemento = valor;
    temp->prox = NULL;

    if(ehvazia(*headPtr)) { //alterada
        *headPtr = temp; //alterada
        *tailPtr = temp; //alterada
    } else {
        (*tailPtr)->prox = temp; //alterada
        *tailPtr = temp; //alterada
    }

}

The same thing is missing from the desinfileira function, and I leave that challenge to you.

    
09.07.2017 / 21:47