document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /*
  2.     Autor: Joel Fernandez
  3.     Ejercicio: Implementacion de una Pila usando Clases
  4.     IDE: Codeblocks
  5.     Web: http://codebotic.blogspot.com
  6.  
  7. */
  8.  
  9. #include<iostream>
  10. #include<cstdlib>
  11. using namespace std;
  12.  
  13. struct nodo{
  14.     int nro;
  15.     struct nodo *sgte;
  16. };
  17.  
  18. typedef struct nodo *Puntero;
  19.  
  20. class Pila{
  21.     public:
  22.         Pila(void);
  23.         void Apilar(int );
  24.         int Desapilar(void );
  25.         void Cima(void);
  26.         bool PilaVacia(void);
  27.         void MostrarPila(void);
  28.         void DestruirPila(void);
  29.  
  30.     private:
  31.         Puntero cima;
  32.  
  33. };
  34. Pila::Pila(void){
  35.     cima=NULL;
  36. }
  37.  
  38. bool Pila::PilaVacia(void){
  39.     if(cima==NULL)
  40.         return true;
  41.     else
  42.         return false;
  43. }
  44.  
  45. void Pila::Apilar(int x){
  46.  
  47.     Puntero p_aux;
  48.     p_aux=new(struct nodo);
  49.     p_aux->nro=x;
  50.     p_aux->sgte=cima;
  51.     cima=p_aux;
  52.  
  53. }
  54.  
  55. void Pila::Cima(){
  56.     int x;
  57.     if(cima==NULL)
  58.         cout<<"\\n\\n\\tPila Vacia...!";
  59.  
  60.     else {
  61.         x=cima->nro;
  62.         cout<<"\\n\\tLa Cima es :"<<x<<endl;
  63.     }
  64. }
  65.  
  66. int Pila::Desapilar(void){
  67.     int x;
  68.     Puntero p_aux;
  69.     if(cima==NULL)
  70.         cout<<"\\n\\n\\tPila Vacia...!!";
  71.     else{
  72.         p_aux=cima;
  73.         x=p_aux->nro;
  74.         cima=cima->sgte;
  75.         delete(p_aux);
  76.     }
  77.     return x;
  78. }
  79.  
  80. void Pila::MostrarPila(void){
  81.     Puntero p_aux;
  82.     p_aux=cima;
  83.  
  84.     while(p_aux!=NULL){
  85.         cout<<"\\t "<<p_aux->nro<<endl;
  86.         p_aux=p_aux->sgte;
  87.     }
  88. }
  89.  
  90. void Pila::DestruirPila(void){
  91.     Puntero p_aux;
  92.  
  93.     while(cima!=NULL){
  94.             p_aux=cima;
  95.             cima=cima->sgte;
  96.             delete(p_aux);
  97.     }
  98. }
  99. void menu(void)
  100. {
  101.     cout<<"\\t -------------------------------------------\\n";
  102.     cout<<"\\t|        IMPLEMENTACION DE UNA PILA         |\\n";
  103.     cout<<"\\t|-------------------------------------------|\\n";
  104.     cout<<" \\t|                                           |"<<endl;
  105.     cout<<" \\t|  1. APILAR                                |"<<endl;
  106.     cout<<" \\t|  2. DESAPILAR                             |"<<endl;
  107.     cout<<" \\t|  3. MOSTRAR PILA                          |"<<endl;
  108.     cout<<" \\t|  4. DESTRUIR PILA                         |"<<endl;
  109.     cout<<" \\t|  5. MOSTRAR CIMA                          |"<<endl;
  110.     cout<<" \\t|  6. SALIR                                 |"<<endl;
  111.     cout<<" \\t|                                           |"<<endl;
  112.     cout<<"\\t -------------------------------------------\\n";
  113.     cout<<"\\t Ingrese opcion: ";
  114. }
  115.  
  116. int main(void ){
  117.  
  118.     system("color 0a");
  119.     Pila pila;
  120.     int x;
  121.     int op;
  122.  
  123.     do
  124.     {
  125.         menu();  cin>> op;
  126.  
  127.         switch(op)
  128.         {
  129.             case 1: cout<< "\\n\\t INGRESE NUMERO A APILAR: "; cin>> x;
  130.                     pila.Apilar(x);
  131.                     cout<<"\\n\\n\\t\\tNumero " << x << " apilado...\\n\\n";
  132.                     break;
  133.  
  134.             case 2:
  135.                     if(pila.PilaVacia()==true)
  136.                         cout<<"\\n\\n\\tPila Vacia....";
  137.                     else{
  138.                         x = pila.Desapilar( );
  139.                         cout<<"\\n\\n\\tNumero "<<x<<" desapilado\\n";
  140.                         }
  141.                     break;
  142.  
  143.  
  144.             case 3:
  145.                     cout << "\\n\\n\\t MOSTRANDO PILA\\n\\n";
  146.                     if(pila.PilaVacia()!=true)
  147.                         pila.MostrarPila(  );
  148.                     else
  149.                         cout<<"\\n\\n\\tPila vacia..!"<<endl;
  150.                     break;
  151.  
  152.  
  153.             case 4:
  154.                     pila.DestruirPila(  );
  155.                     cout<<"\\n\\n\\t\\tPila eliminada...\\n\\n";
  156.                     break;
  157.             case 5:
  158.                     pila.Cima();
  159.                     break;
  160.          }
  161.  
  162.         cout<<endl<<endl;
  163.         system("pause");  system("cls");
  164.  
  165.     }while(op!=6);
  166.  
  167. return 0;
  168. }
');