Advertisement
Gabriel_Rofl

test.c

Oct 23rd, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.43 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.  
  45. int pilha_vazia(t_pilha* pilha){
  46.     return(pilha->topo == NULL);
  47. }
  48.  
  49. int tam_pilha(t_pilha* pilha){
  50.     int counter = 0;
  51.     t_ele* atual = pilha->topo;
  52.     while(atual != NULL){
  53.         atual = atual->ant;
  54.         counter++;
  55.     }
  56.     return counter;
  57. }
  58.  
  59. void print_pilha(t_pilha* pilha){
  60.     t_ele* aux = pilha->topo;
  61.     if(pilha_vazia(pilha)){
  62.         printf("Pilha vazia!\n");
  63.         return;
  64.     }
  65.     while(aux != NULL){
  66.         printf("%d <- ", pilha->topo->dado);
  67.         pilha->topo = pilha->topo->ant;
  68.  
  69.         aux = aux->ant;
  70.     }
  71.     printf(" NULL\n");
  72. }
  73.  
  74. void libera_pilha(t_pilha* pilha){
  75.     while(pilha->topo != NULL){
  76.         t_ele* aux = pilha->topo;
  77.         pilha->topo = pilha->topo->ant;
  78.         free(aux);
  79.     }
  80.     free(pilha);
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement