Advertisement
Gabriel_Rofl

test.c

Oct 22nd, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.15 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "test.h"
  4.  
  5.  
  6. t_pilha* aloca_pilha(){
  7.     t_pilha* ptr = (t_pilha *) malloc(sizeof(t_pilha));
  8.     ptr->topo = NULL;
  9.     return ptr;
  10. }
  11.  
  12. t_ele* aloca_elemento(int valor){
  13.     t_ele* ptr = (t_ele *) malloc(sizeof(t_ele));
  14.     ptr->dado = valor;
  15.     ptr->ant = NULL;
  16.  
  17.     return ptr;
  18. }
  19.  
  20.  
  21. void push(t_pilha* pilha, int valor){
  22.  
  23.     t_ele* elemento = aloca_elemento(valor);
  24.     if(pilha_vazia(pilha)){
  25.         pilha->topo = elemento;
  26.         elemento->ant = NULL;
  27.     }
  28.     else{
  29.         elemento->ant = pilha->topo;
  30.         pilha->topo = elemento;
  31.     }
  32. }
  33.  
  34. int pop(t_pilha* pilha, t_ele* elemento){
  35.     if(pilha_vazia(pilha)){
  36.         printf("A pilha esta vazia\n");
  37.         return 0;
  38.     }
  39.     t_ele* aux = pilha->topo;
  40.     pilha->topo = elemento->ant;
  41.     free(aux);
  42. }
  43.  
  44. int pilha_vazia(t_pilha* pilha){
  45.     return(pilha->topo == NULL);
  46. }
  47.  
  48. int tam_pilha(t_pilha* pilha){
  49.     int counter = 0;
  50.     t_ele* atual = pilha->topo;
  51.     while(atual != NULL){
  52.         atual = atual->ant;
  53.         counter++;
  54.     }
  55.     return counter;
  56. }
  57.  
  58.  
  59. void libera_pilha(t_pilha* pilha, t_ele* elemento){
  60.     while(pilha->topo != NULL){
  61.         t_ele* aux = pilha->topo;
  62.         pilha->topo = elemento->ant;
  63.         free(aux);
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement