Skip to content

Instantly share code, notes, and snippets.

@folivetti
Created March 9, 2023 19:59
Show Gist options
  • Save folivetti/2c1ca09ce1d7a1529eb491f09971a750 to your computer and use it in GitHub Desktop.
Save folivetti/2c1ca09ce1d7a1529eb491f09971a750 to your computer and use it in GitHub Desktop.
#include <stdlib.h>
#include "lista.h"
#include "pilha.h"
PilhaDinamica *criar_pilha() {
PilhaDinamica *pilha = malloc(sizeof(PilhaDinamica));
pilha->topo = NULL;
return pilha;
}
int empilhar(PilhaDinamica *pilha, int valor) {
LinkedNode *novo_item = malloc(sizeof(LinkedNode));
if (novo_item == NULL) return 0;
novo_item->data = valor;
novo_item->next = pilha->topo;
pilha->topo = novo_item;
return 1;
}
int desempilhar(PilhaDinamica *pilha, int *valor) {
// Pilha vazia?
if (pilha->topo == NULL) return 0;
LinkedNode *no = pilha->topo;
pilha->topo = pilha->topo->next;
*valor = no->data;
free(no);
return 1;
}
void liberar_lista_pilha(LinkedNode *no) {
if (no == NULL) return;
liberar_lista_pilha(no->next);
free(no);
}
void liberar_pilha(PilhaDinamica *pilha) {
liberar_lista_pilha(pilha->topo);
free(pilha);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment