Guest User

Untitled

a guest
Jul 16th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct _pilha{
  5. int *vetor;
  6. int topo;
  7. int limite;
  8. }TpPilha;
  9.  
  10. TpPilha * criarPilha(int size){
  11. TpPilha *p = (TpPilha *)malloc(sizeof(TpPilha));
  12. if(p){
  13. p->vetor = (int *)calloc(size,sizeof(int));
  14. }else{
  15. return NULL;
  16. }
  17. if(p->vetor){
  18. p->topo = -1;
  19. p->limite = size-1;
  20. return p;
  21. }else{
  22. return NULL;
  23. }
  24. }
  25.  
  26. int eCheia(TpPilha *p,int size){
  27. if(p->topo == size){
  28. return 1;
  29. }else{
  30. return 0;
  31. }
  32. }
  33.  
  34. int eVazia (TpPilha *p){
  35. if(p->topo == -1){
  36. return 1;
  37. }else{
  38. return 0;
  39. }
  40. }
  41.  
  42. int Push(TpPilha *p,int e){
  43. if(eCheia(p,e)==0){
  44. p->topo++;
  45. p->vetor[p->topo] = e;
  46. return 1;
  47. }else{
  48. printf("Pilha cheia!");
  49. return 0;
  50. }
  51. }
  52.  
  53. int Pop(TpPilha *p){
  54. if(eVazia(p)==0){
  55. p->topo--;
  56. return 1;
  57. }else{
  58. printf("Pilha Vazia!");
  59. return 0;
  60. }
  61. }
  62.  
  63. int Exibe(TpPilha *p){
  64. if(eVazia(p)!=1){
  65. int i;
  66. for(i=p->topo;i>-1;i--){
  67. printf("\n%d",p->vetor[i]);
  68. }
  69. }else{
  70. printf("Pilha vazia!");
  71. }
  72. }
  73.  
  74. int Consulta(TpPilha *p){
  75. if(eVazia(p)!=1){
  76. printf("Topo: %d",p->vetor[p->topo]);
  77. return 1;
  78. }else{
  79. printf("Pilha vazia!");
  80. return 0;
  81. }
  82. }
  83.  
  84.  
  85.  
  86. int main(){
  87.  
  88.  
  89.  
  90. int size,num,op = 0;
  91.  
  92. printf("Informe o tamanho da Pilha");
  93. scanf("%d",&size);
  94. TpPilha *p;
  95. criarPilha(size);
  96. p=criarPilha(size);
  97.  
  98. do{
  99.  
  100. printf("\n\t----------Menu----------");
  101. printf("\n\t1: Inserir");
  102. printf("\n\t2: Extrair");
  103. printf("\n\t3: Exibir");
  104. printf("\n\t4: Consultar");
  105. printf("\n\t5: Sair");
  106. printf("\n\t------------------------");
  107. printf("\n\tEscolha uma Opcao");
  108. scanf("%d",&op);
  109. switch(op){
  110.  
  111. case 1:
  112. printf("Digite o elemento a ser inserido");
  113. scanf("%d",&num);
  114. Push(p,num);
  115. break;
  116.  
  117. case 2:
  118. Pop(p);
  119. break;
  120.  
  121. case 3:
  122. Exibe(p);
  123. break;
  124.  
  125. case 4:
  126. Consulta(p);
  127. break;
  128.  
  129.  
  130.  
  131. }
  132. }while(op!=5);
  133. free(p);
  134.  
  135. }
Add Comment
Please, Sign In to add comment