Advertisement
Gabriel_Rofl

Untitled

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