0
I need to create a function that prints a reverse stack. I did the void implementation pilha_inverted (Stack *p), but when I use pilha_pop the execution stops and is finished. The problem does not occur when using the pilha_displays function instead of the reversed pilha_function.
void pilha_invertida(Pilha *p) {
Pilha *aux, *aux2 = pilha_cria();
aux = p;
char c[8];
while(!pilha_vazia(aux)) {
strcpy(c, pilha_pop(aux));
pilha_push(aux2, c);
}
pilha_exibe(aux2);
}
pilha_pop
char* pilha_pop(Pilha* p){
Elemento* t;
char *v;
if(pilha_vazia(p)){
//printf("Pilha vazia.\n");
//return 0;
}
t = p->prim;
v = t->descricao;
p->prim= t->prox;
free(t);
p->q = p->q - 1;
return v;
}
struct:
typedef struct elemento{
char descricao[8];
struct elemento* prox;
}Elemento;
typedef struct pilha{
int q;
Elemento* prim;
}Pilha;
main:
int main() {
Pilha **p;
int nf = 2, nc = 3;
p = (Pilha**) malloc(nf * sizeof(Pilha*));
for(i = 0; i < nf; i++){
p[i] = (Pilha*) malloc(nc * sizeof(Pilha));
p[i] = pilha_cria();
}
pilha_push(p[0], "ABCD");
pilha_push(p[0], "QWER");
pilha_pop(p[0]);
pilha_push(p[0], "POIU");
pilha_invertida(p[0]);
}
In the listing functions, I forgot to put a if to check whether the pile it’s empty, but that’s on you :).
– user201641