#include #include #define TIPO int #define IMPRIME_VALOR(X) printf("%d", X) struct struct_pilha_ll { TIPO valor; struct struct_pilha_ll *prox; }; typedef struct struct_pilha_ll pilha_ll; pilha_ll* cria_pilha_ll() { pilha_ll *ret = malloc (sizeof(pilha_ll)); ret->prox = NULL; return ret; } void destroi_pilha_ll(pilha_ll *p) { if (p->prox != NULL) { printf("Pilha nao vazia\n"); exit(1); } free(p); } void push_ll(pilha_ll *p, TIPO x) { pilha_ll *celula = malloc(sizeof(pilha_ll)); celula->valor = x; celula->prox = p->prox; p->prox = celula; } TIPO pop_ll(pilha_ll *p) { TIPO ret; pilha_ll *rem = p->prox; if (rem == NULL) { printf("Pilha vazia (stack underflow)"); exit(1); } p->prox = rem->prox; ret = rem->valor; free(rem); return ret; } TIPO peek_ll(pilha_ll *p) { pilha_ll *topo = p->prox; if (topo == NULL) { printf("Pilha vazia (stack underflow)"); exit(1); } return p->valor; } void imprime_pilha(pilha_ll *p) { printf("P: "); p = p->prox; //descarta a cabeça while (p) { IMPRIME_VALOR(p->valor); printf(" "); p = p->prox; } printf("\n"); } int main (int argc, char** argv) { pilha_ll* ps1 = cria_pilha_ll(5); pilha_ll* ps2 = cria_pilha_ll(5); push_ll(ps1, 10); push_ll(ps2, 20); push_ll(ps1, 30); push_ll(ps1, pop_ll(ps2)); printf("Pilha 1 - "); imprime_pilha(ps1); printf("Pilha 2 - "); imprime_pilha(ps2); pop_ll(ps1); pop_ll(ps1); pop_ll(ps1); destroi_pilha_ll(ps1); destroi_pilha_ll(ps2); }