Advertisement
argentinapb

Untitled

Apr 10th, 2019
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. #define max 5
  4.  
  5. struct pilha{
  6. int topo;
  7. int pilha[max];
  8. }p;
  9.  
  10. void push (int x, struct pilha *p){
  11. if(p->topo == max-1)
  12. printf("a pilha esta cheia\n");
  13.  
  14. else
  15. p->pilha[++p->topo] = x;
  16. }
  17.  
  18. int pop (struct pilha *p){
  19. p->topo--;
  20. }
  21.  
  22. void verificar(struct pilha *p){
  23.  
  24. printf("o topo da pilha eh: %d\n", p->pilha[p->topo]);
  25. }
  26.  
  27. void mostrar (struct pilha *p){
  28. int aux[max];
  29. int topoaux = -1;
  30.  
  31. if(p->topo != -1){
  32.  
  33. for(int i = 0; p->topo >= i; p->topo--,topoaux++){
  34. printf("%d\n", p->pilha[p->topo]);
  35. aux[topoaux] = p->pilha[p->topo];
  36.  
  37.  
  38. }
  39. for(int i = 0; topoaux >= 0; topoaux--){
  40. p->pilha[p->topo] = aux[topoaux];
  41. p->topo++;
  42. }
  43. }
  44. else
  45. printf("nao ha elementos a ser mostrado\n");
  46. }
  47.  
  48. void main(){
  49. int n, x;
  50. p.topo = -1;
  51. do{
  52. printf("1 - Inserir elementos na pilha\n2 - Retirar elementos na pilha\n3 - Verificar o elemento que esta no topo da pilha\n4 - Mostrar todos elementos da pilha\n0 - Sair\n");
  53. scanf("%d", &n);
  54.  
  55. switch(n){
  56.  
  57. case 1:
  58. printf("digite o valor que deseja inserir na pilha\n");
  59. scanf("%d", &x);
  60. push(x, &p);
  61. break;
  62.  
  63. case 2:
  64. pop(&p);
  65. break;
  66.  
  67. case 3:
  68. verificar(&p);
  69. break;
  70.  
  71. case 4:
  72. mostrar(&p);
  73. break;
  74.  
  75. case 0:
  76. return;
  77.  
  78. default:
  79. printf("valor invalido\n");
  80.  
  81. }
  82.  
  83. }while(n != 0);
  84. return;
  85.  
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement