Created
March 9, 2023 19:59
-
-
Save folivetti/2c1ca09ce1d7a1529eb491f09971a750 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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