document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /*
  2.     Autor: Joel Fernandez Segura
  3.     Tema : Colas
  4.     Ejercicio 1 :Implementar una cola con todas sus operaciones
  5.     IDE: Codeblocks
  6. */
  7.  
  8. #include<iostream>
  9. #include<cstdlib>
  10. using namespace std;
  11.  
  12. struct nodo{
  13.     int nro;
  14.     struct nodo *sgte;
  15. };
  16.  
  17. typedef struct nodo *Puntero;
  18.  
  19. class Cola{
  20.     public:
  21.         Cola(void);
  22.         void Encolar(int );
  23.         int Desencolar(void );
  24.         bool ColaVacia(void);
  25.         void MostrarCola(void);
  26.         void VaciarCola(void);
  27.  
  28.     private:
  29.         Puntero delante;//puntero al primer elemento de la cola
  30.         Puntero atras;//puntero al ultimo elemento de la cola
  31.  
  32. };
  33.  
  34.  
  35. Cola::Cola(void){
  36.     delante=NULL;//inicializamos los punteros
  37.     atras=NULL;
  38. }
  39.  
  40. bool Cola::ColaVacia(){
  41.  
  42.     if(delante==NULL)
  43.         return true;
  44.     else return false;
  45. }
  46. void Cola::Encolar(int x){
  47.  
  48.      Puntero p_aux;
  49.  
  50.      p_aux=new(struct nodo);
  51.      p_aux->nro=x;
  52.      p_aux->sgte=NULL;
  53.  
  54.      if(delante==NULL)
  55.         delante=p_aux;
  56.      else atras->sgte=p_aux;
  57.  
  58.      atras=p_aux;
  59. }
  60.  
  61. int Cola::Desencolar(void){
  62.  
  63.     int n;
  64.     Puntero p_aux;
  65.  
  66.     p_aux=delante;
  67.     n=p_aux->nro;
  68.     delante=(delante)->sgte;
  69.     delete(p_aux);
  70.     return n;
  71. }
  72.  
  73. void Cola::MostrarCola(void){
  74.     Puntero p_aux;
  75.      p_aux=delante;
  76.      while(p_aux!=NULL){
  77.         cout<<"\\n\\n\\t"<<p_aux->nro;
  78.         p_aux=p_aux->sgte;
  79.      }
  80.  
  81. }
  82.  
  83. void Cola::VaciarCola(void){
  84.  
  85.      Puntero p_aux,r_aux;
  86.      p_aux=delante;
  87.      while(p_aux!=NULL){
  88.         r_aux=p_aux;
  89.         p_aux=p_aux->sgte;
  90.         delete(r_aux);
  91.      }
  92.      delante=NULL;
  93.      atras=NULL;
  94. }
  95.  
  96. int menu(void)
  97. {
  98.     cout<<"\\n\\n\\t -------------------------------------------\\n";
  99.     cout<<"\\t|        IMPLEMENTACION DE UNA COLA         |\\n";
  100.     cout<<"\\t|-------------------------------------------|\\n";
  101.     cout<<" \\t|                                           |"<<endl;
  102.     cout<<" \\t|  1. ENCOLAR                               |"<<endl;
  103.     cout<<" \\t|  2. DESENCOLAR                            |"<<endl;
  104.     cout<<" \\t|  3. MOSTRAR COLA                          |"<<endl;
  105.     cout<<" \\t|  4. VACIAR COLA                           |"<<endl;
  106.     cout<<" \\t|  5. SALIR                                 |"<<endl;
  107.     cout<<" \\t|                                           |"<<endl;
  108.     cout<<"\\t -------------------------------------------\\n";
  109.     cout<<"\\t Ingrese opcion: ";
  110. }
  111. int main(void ){
  112.  
  113.     system("color 1f");
  114.     Cola cola;
  115.     int x;
  116.     int op;
  117.  
  118.     do
  119.     {
  120.         menu();  cin>> op;
  121.  
  122.         switch(op)
  123.         {
  124.             case 1: cout<< "\\n\\t INGRESE NUMERO A ENCOLAR: "; cin>> x;
  125.                     cola.Encolar(x);
  126.                     cout<<"\\n\\n\\t\\tNumero " << x << " ENCOLADO...\\n\\n";
  127.                     break;
  128.  
  129.             case 2:
  130.                     if(cola.ColaVacia()==true)
  131.                         cout<<"\\n\\n\\tCola Vacia....";
  132.                     else{
  133.                         x = cola.Desencolar( );
  134.                         cout<<"\\n\\n\\tNumero "<<x<<" Desencolado\\n";
  135.                         }
  136.                     break;
  137.  
  138.  
  139.             case 3:
  140.                     cout << "\\n\\n\\t MOSTRANDO COLA\\n\\n";
  141.                     if(cola.ColaVacia()!=true)
  142.                         cola.MostrarCola(  );
  143.                     else
  144.                         cout<<"\\n\\n\\tCola vacia..!"<<endl;
  145.                     break;
  146.  
  147.  
  148.             case 4:
  149.                     cola.VaciarCola(  );
  150.                     cout<<"\\n\\n\\t\\tCola eliminada...\\n\\n";
  151.                     break;
  152.  
  153.             default: cout<<"\\n\\tIngrese una Opcion Valida....!!";
  154.          }
  155.  
  156.         cout<<endl<<endl;
  157.         system("pause");  system("cls");
  158.  
  159.     }while(op!=5);
  160.  
  161. return 0;
  162. }
');