Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.51 KB | None | 0 0
  1. /*
  2. * C++ - Pilas/Stack
  3. * Copyright 2014 Martin Cruz Otiniano
  4. * Description : Apila elemento, Desempila elemento, Mostrar pila, Destruir Pila
  5. * Site        : martincruz.me
  6. */
  7.  
  8. #include <iostream>
  9. using namespace std;
  10.  
  11. struct nodo{
  12.     int nro;
  13.     struct nodo *sgte;
  14. };
  15.  
  16. typedef nodo *ptrPila;   // creando nodo tipo puntero( tipo de dato )
  17.  
  18. /*                        Apilar elemento      
  19. ------------------------------------------------------------------------*/
  20. void push( ptrPila &p, int valor )
  21. {
  22.      ptrPila aux;
  23.      aux = new(struct nodo);  // apuntamos al nuevo nodo creado
  24.      aux->nro = valor;
  25.      
  26.      aux->sgte = p ;
  27.      p = aux ;
  28. }
  29.  
  30. /*                Desapilar elemento(devuelve elemento)      
  31. ------------------------------------------------------------------------*/
  32. int pop( ptrPila &p )
  33. {
  34.      int num ;
  35.      ptrPila aux;
  36.      
  37.      aux = p ;
  38.      num = aux->nro;   // asignamos el primer vamor de la pila
  39.      
  40.      p = aux->sgte ;
  41.      delete(aux);
  42.      
  43.      return num;
  44. }
  45.  
  46. /*                     Muestra elementos de la pila      
  47. ------------------------------------------------------------------------*/
  48. void mostrar_pila( ptrPila p )
  49. {
  50.      ptrPila aux;
  51.      aux = p;     // apunta al inicio de la lista
  52.      
  53.      while( aux !=NULL )
  54.      {
  55.             cout<<"\t"<< aux->nro <<endl;
  56.             aux = aux->sgte;
  57.      }    
  58. }
  59.  
  60. /*                Eliminar todos los elementos de la pila      
  61. ------------------------------------------------------------------------*/
  62. void destruir_pila( ptrPila &p)
  63. {
  64.      ptrPila aux;
  65.      
  66.      while( p != NULL)
  67.      {
  68.            aux = p;
  69.            p = aux->sgte;
  70.            delete(aux);
  71.      }
  72. }
  73.  
  74. /*                        Menu de opciones    
  75. ------------------------------------------------------------------------*/
  76. void menu()
  77. {
  78.     cout<<"\n\t IMPLEMENTACION DE PILAS EN C++\n\n";
  79.     cout<<" 1. APILAR                                "<<endl;
  80.     cout<<" 2. DESAPILAR                             "<<endl;
  81.     cout<<" 3. MOSTRAR PILA                          "<<endl;
  82.     cout<<" 4. DESTRUIR PILA                         "<<endl;
  83.     cout<<" 5. SALIR                                 "<<endl;
  84.  
  85.     cout<<"\n INGRESE OPCION: ";
  86. }
  87.  
  88. /*                        Funcion Principal        
  89. ------------------------------------------------------------------------*/
  90. int main()
  91. {
  92.     ptrPila p = NULL;  // creando pila
  93.     int dato;
  94.     int op;
  95.     int x ; // numero que devuelve la funcon pop
  96.    
  97.     system("color 0b");
  98.  
  99.     do
  100.     {
  101.         menu();  cin>> op;
  102.  
  103.         switch(op)
  104.         {
  105.             case 1:
  106.  
  107.                  cout<< "\n NUMERO A APILAR: "; cin>> dato;
  108.                  push( p, dato );
  109.                  cout<<"\n\n\t\tNumero " << dato << " apilado...\n\n";
  110.             break;
  111.  
  112.  
  113.             case 2:
  114.  
  115.                  x = pop( p );
  116.                  cout<<"\n\n\t\tNumero "<< x <<" desapilado...\n\n";
  117.             break;
  118.                  
  119.  
  120.             case 3:
  121.  
  122.                  cout << "\n\n MOSTRANDO PILA\n\n";
  123.                  if(p!=NULL)
  124.                     mostrar_pila( p );
  125.                  else
  126.                     cout<<"\n\n\tPila vacia..!"<<endl;
  127.             break;
  128.  
  129.  
  130.             case 4:
  131.  
  132.                  destruir_pila( p );
  133.                  cout<<"\n\n\t\tPila eliminada...\n\n";
  134.             break;
  135.            
  136.          }
  137.  
  138.         cout<<endl<<endl;
  139.         system("pause");  system("cls");
  140.  
  141.     }while(op!=5);
  142.    
  143.    
  144.     return 0;
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement