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ção
line`, 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);
}
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 ?
– Otavio Augusto
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.– Isac