#include <stdio.h> #include <stdlib.h> struct no { int v; struct no* prox; }; typedef struct no No; struct pilha { No* inicio; }; typedef struct pilha Pilha; void imprime_pilha(Pilha f) { No* l = f.inicio; printf("("); if (l != NULL) { printf("%d", l->v); l = l->prox; } while (l != NULL) { printf(", %d", l->v); l = l->prox; } printf(")\n"); } void inicia_pilha (Pilha *ap_p) { ap_p->inicio = NULL; } void insere_pilha(Pilha *ap_p, int v) { No *n; n = (No*) malloc(sizeof(No)); n->v = v; n->prox = ap_p->inicio; ap_p->inicio = n; } /* Retorna o valor do elemento removido ou -1 caso a pilha esteja vazia. */ int remove_pilha(Pilha *p) { No* n; int v; if (p->inicio == NULL) return -1; n = p->inicio; p->inicio = n->prox; v = n->v; free(n); return v; } int main() { Pilha f; inicia_pilha(&f); insere_pilha(&f, 1); imprime_pilha(f); insere_pilha(&f, 3); imprime_pilha(f); insere_pilha(&f, 5); imprime_pilha(f); while (remove_pilha(&f) != -1) imprime_pilha(f); return 0; }