Advertisement
GuiBrust

Untitled

Oct 2nd, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.49 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define TAMANHO 10
  4. struct VaPilha{
  5.     int Item[TAMANHO]; //tipo de variavel a ser armazenada
  6.     int TOPO; //Posição do vetor
  7. };
  8.  
  9. void Inicia(VaPilha *p){ //inicia pilha
  10.     p->TOPO=-1;   //passa pilha como referencia
  11. }
  12.  
  13. int VaPilha_vazia(VaPilha *p){
  14.     if(p->TOPO==-1){
  15.         return 1; //pilha vazia retorna 1}
  16.     else{
  17.         return 0; //caso contrario 0}
  18. }
  19.  
  20. int VaPilha_Cheia(VaPilha *p){
  21.     if(p->TOPO == TAMANHO){
  22.         return 1; //Pilha cheia retorna 1}
  23.     else{
  24.         return 0;}
  25. }
  26.  
  27. void VaPilha_Insere(VaPilha *p, int x){
  28.     if(VaPilha_Cheia(p)==1){//verifica topo, passa como parametro a pilha
  29.         printf("Pilha cheia\n");}
  30.     else{
  31.            p->TOPO++;
  32.            p->Item[p->TOPO]=x;  //x é o valor passado
  33.     }
  34. }
  35.  
  36. int VaPilha_Retira(VaPilha *p){
  37.     int AUX=0;
  38.     if(VaPilha_vazia(p)==1){
  39.         printf("Pilha vazia\n");}
  40.     else{
  41.         AUX=p->Item[p->TOPO];
  42.         p->TOPO--; //diminui topo
  43.         return AUX; //retorna valor tirado}
  44. }
  45. int main(int argc, char** argv){
  46.     VaPilha Pilha; //define pilha -- nas funçoes 'p'
  47.     Inicia(&Pilha); //inicia mandando o endereço como parametro
  48.     VaPilha_Insere(&Pilha, 1);
  49.     VaPilha_Insere(&Pilha, 2);
  50.     VaPilha_Insere(&Pilha, 3);
  51.    
  52.     int aux;                            //saida
  53.     aux=VaPilha_Retira(&Pilha);
  54.     printf("SAIU %d\n", aux);          //SAIU 3
  55.     aux=VaPilha_Retira(&Pilha);
  56.     printf("SAIU %d\n", aux);           //SAIU 2
  57.     aux=VaPilha_Retira(&Pilha);
  58.     printf("SAIU %d\n", aux);           //SAIU 1
  59.     aux=VaPilha_Retira(&Pilha);
  60.     printf("SAIU %d\n", aux);       //SAIU 1       //SAIU 0
  61. return 0;}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement