Glaas2

Pila Estatica

Mar 22nd, 2012
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.37 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. #define TAMMAX 10
  5.  
  6. typedef struct PilaEstatica
  7. {
  8.  int tope;
  9.  int elementos[TAMMAX];
  10. }pila;
  11.  
  12. pila Pila;
  13.  
  14. //Para inicializar la pila
  15. void Initialize(pila *p)
  16. {
  17.  p->tope=0;
  18. }
  19.  
  20. int IsEmpty(pila *p)
  21. {
  22.  if((p->tope)==0)
  23.     return 1;
  24.  else
  25.     return 0;  
  26. }
  27.  
  28. int IsFull(pila *p)
  29. {
  30.    if((p->tope)==TAMMAX)
  31.       return 1;
  32.    else
  33.       return 0;  
  34. }
  35.  
  36. int Push(pila *p, int dato)
  37. {
  38.    if(IsFull(p))
  39.       return 1;
  40.    p->elementos[p->tope]=dato;
  41.    p->tope++;
  42.    return 1;
  43. }
  44.  
  45. int Pop(pila *p, int dato)
  46. {
  47.    if(IsEmpty(p))
  48.       return 0;
  49.    else
  50.       dato=p->elementos[p->tope-1];
  51.       p->tope--;
  52. }
  53.  
  54. int Purge(pila *p)
  55. {
  56.    int cont;
  57.    if(IsEmpty(p))
  58.       return 0;
  59.    else
  60.    for(cont=0;cont<=TAMMAX;cont++)      
  61.       {
  62.     p->elementos[cont]=0;
  63.       p->tope--;
  64.      }
  65. }
  66. int Show(pila *p)
  67. {
  68.    int cont;
  69.    if(IsEmpty(p))
  70.       return 0;
  71.    else
  72.    for(cont=0;cont<TAMMAX;cont++)      
  73.       {
  74.     printf("%d.-  %d  \n", cont+1,  p->elementos[cont]);
  75.      }
  76. }
  77.  
  78. void menu(int *popc)
  79. {
  80. printf("\n Menu \n \n");
  81. printf(" 1.- Ingresar Dato\n ");
  82. printf("2.- Quitar Dato\n ");
  83. printf("3.- Mostrar Tope\n ");
  84. printf("4.- Mostrar Pila\n ");
  85. printf("5.- Borrar Pila\n ");
  86. scanf("%d", popc);
  87. }
  88.  
  89. main()
  90. {
  91.     int dato, opc, cont, mas;
  92.    Initialize(&Pila);
  93.    menu(&opc);
  94.   switch(opc)
  95. {
  96.         case 1:
  97.                 for(cont=1;cont<TAMMAX;cont++)                
  98.                  {
  99.                  printf("Ingrese un dato: ");
  100.                  scanf("%d",  &dato);
  101.                  Push(&Pila, dato);
  102.                  printf("\nDesea ingresar otro dato? (1/0)");
  103.                  scanf("%d", &mas);
  104.                      if(mas==0)                              
  105.                               break;        
  106.                        
  107.                 }
  108.                 break;
  109.         case 2:
  110.                 for(cont=1;cont<TAMMAX;cont++)                
  111.                  {
  112.                  printf("Ingrese el primer dato: ");
  113.                  scanf("%d",  &dato);
  114.                  Pop(&Pila, dato);
  115.                  printf("\nDesea quitar otro dato? (1/0)");
  116.                  scanf("%d", &mas);
  117.                      if(mas==0)                              
  118.                               break;              
  119.                 }
  120.                break;
  121. }      
  122.  
  123.      
  124. }
Advertisement
Add Comment
Please, Sign In to add comment