Advertisement
Guest User

pilha.c

a guest
Apr 5th, 2020
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.08 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "pilha.h"
  4.  
  5. struct pilha{
  6.    
  7.     int *vetor; //elemento
  8.     int tp, qtde; //size, capacity
  9.    
  10. };
  11.  
  12. TPilha *criar(int qtde){
  13.    
  14.     TPilha *p = (TPilha*)malloc(sizeof(TPilha));
  15.     p->qtde = qtde;
  16.     p->vetor = (int *)malloc(qtde * sizeof(int));
  17.     p->tp = -1;
  18.     return p;
  19.    
  20. }
  21.  
  22. int esta_vazia(TPilha *p){
  23.    
  24.     return p->tp == -1;
  25. }
  26.  
  27. int esta_cheia(TPilha *p){
  28.    
  29.     return p->tp == p->tp - 1;
  30.    
  31. }
  32.  
  33. void empilhar(TPilha *p,int x){
  34.    
  35.     if(esta_cheia(p)){  
  36.      
  37.       printf("Pilha está cheia.");
  38.       return;
  39.      
  40.     }
  41.    
  42.     p->tp++;      
  43.     p->vetor[p->tp] = x;
  44.        
  45. }
  46.  
  47.  
  48. int desempilhar (TPilha *p){
  49.    
  50.     if (esta_vazia(p)){
  51.        
  52.         printf("A Pilha está vazia.");
  53.         return -1;
  54.                    
  55.     }
  56.    
  57.     p->tp--;       
  58.     return p->vetor[p->tp+1];
  59.    
  60.    
  61. }
  62.  
  63. int tamanho(TPilha *p){
  64.    
  65.     return p->tp;
  66. }
  67.  
  68. int topo(TPilha *p){
  69.    
  70.     return p->vetor[p->tp];
  71. }
  72.  
  73. void exibe(TPilha *p){
  74.    
  75.     int i;
  76.    
  77.     if(esta_vazia(p)){
  78.        
  79.         printf("A pilha esta vazia.");
  80.         return;
  81.        
  82.     }
  83.    
  84.     for(i = 0; i <= p->tp; i++){
  85.        
  86.         printf("%d\n", p->vetor[i]);
  87.        
  88.     }
  89.    
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement