row items in a row (pointers)

Asked

Viewed 614 times

1

Hello, I am implementing a function to insert elements in a queue
and I realized that after the first element inserted, the pointer headPtr and tailPtrestão apontando para o mesmo lugar, logo o problema só pode estar na funçãoline`, but I can’t solve it. Could someone point out her mistake ? Follow 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);

}

1 answer

0


The first error appears in:

tailPtr = head; 

Inside the function lines because head does not exist, has to be headPtr or temp. Then at the end of the function desinfileira has:

}tailPtr = temp;

Which also can not be because outside the function the node temp does not exist once it was created within 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 receives the pointer to the FILA, but this is a copy of the pointer that is in main, thus an instruction of this genus:

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

It does not have the desired effect as it does not change the main pointer. A common solution to this problem is to return the new value to the pointer, but as we have two pointers 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 on the job 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
    }

}

Now we need to apply the same principle to function desinfileira, and I leave that challenge to you.

  • Isac. Thank you very much, I changed the code here, it worked perfectly. But I made a list simply chained using pointers, I even took a question here in the forum, and I didn’t use reference to solve the problem, and it worked too. So why in my code should I use the reference passage ?

  • Because it has the pointers on main, which I assumed you wanted. Another solution is to put the objects in the main instead of pointers, and the function receiving the pointers already works. But with normal main pointer and normal no pointer enfileira doesn’t work.

Browser other questions tagged

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