Advertisement
fabgonber

Untitled

Oct 26th, 2020
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.93 KB | None | 0 0
  1. /* INICIO DE main.cpp */
  2. #include <iostream>
  3. #include "colaint.h"
  4. #include "colagenerica.h"
  5.  
  6. using namespace std;
  7.  
  8. template<class TipoDato>
  9. void imprimir(Cola<TipoDato> & laCola)
  10. {
  11.     while(!laCola.vacia())
  12.     {
  13.         cout << "Valor: " << laCola.extraer() << endl;
  14.     }
  15. }
  16.  
  17. void ejemplo_cuatro()
  18. {
  19.     int i;
  20.     cout << "Clase Cola Generica (implementacion estatica)" << endl;
  21.     cout << "(usando plantilla de clases) " << endl << endl;
  22.  
  23.     Cola<int> unaCola;
  24.     unaCola.agregar(1);
  25.     unaCola.agregar(2);
  26.     unaCola.agregar(3);
  27.  
  28.     cout << " e = " << unaCola.extraer() << endl;
  29.     cout << "Vacio? " << unaCola.vacia() << endl;
  30.     cout << " e = " << unaCola.extraer() << endl;
  31.     cout << " e = " << unaCola.extraer() << endl;
  32.     cout << "Vacio? " << unaCola.vacia() << endl << endl;
  33.  
  34.  
  35.     for(i=1; i<7; i++)
  36.         unaCola.agregar(i*2);
  37.  
  38.     imprimir(unaCola);
  39.     imprimir(unaCola);
  40.  
  41.     cout << endl << " Ejemplo Cola Float " << endl;
  42.     Cola<float> otra_cola;
  43.     otra_cola.agregar(2.5);
  44.     otra_cola.agregar(3.0);
  45.     imprimir(otra_cola);
  46.  
  47.     cout << endl << " Ejemplo Cola strig " << endl;
  48.     Cola<string> mi_cola;
  49.     mi_cola.agregar("pedro");
  50.     mi_cola.agregar("juan");
  51.     imprimir(mi_cola);
  52. }
  53.  
  54.  
  55.  
  56. int main() {
  57.  
  58.     cout << "\n=======================\n";
  59.     ejemplo_cuatro();
  60.    
  61.  
  62. }
  63. /* FIN DE main.cpp */
  64.  
  65. /* inicio de colagenerica.cpp */
  66. #include "colagenerica.h"
  67. /* fin de colagenerica.cpp */
  68.  
  69. /* inicio de colagenerica.h */
  70. #ifndef COLAGENERICA_H
  71. #define COLAGENERICA_H
  72. template<class TipoDato>
  73. class Cola
  74. {
  75.     public:
  76.         Cola()
  77.         {
  78.             tope = 0;
  79.             ppio = 0;
  80.         }
  81.        
  82.         void agregar(TipoDato elemento)
  83.         {
  84.             vector[tope] = elemento;
  85.             tope++;
  86.         }
  87.  
  88.         TipoDato extraer()
  89.         {
  90.               TipoDato elemento;
  91.               elemento = vector[ppio];
  92.               ppio++;
  93.              
  94.              
  95.  
  96.               this->garbagecolector();
  97.              
  98.               return elemento;
  99.         }
  100.        
  101.         bool vacia()
  102.         {
  103.           return (tope == ppio);
  104.         }
  105.    
  106.         bool vacio()
  107.         {
  108.           return this->vacia();
  109.         }
  110.  
  111.     protected:
  112.         void garbagecolector()
  113.         {
  114.             int i, restar;
  115.             if (tope == ppio)
  116.             {
  117.                 tope = 0;
  118.                 ppio = 0;
  119.             }
  120.             else
  121.             {
  122.               if (ppio>50)
  123.               {
  124.                 restar = ppio;
  125.                 for (i=ppio;i<tope;i++)
  126.                 {
  127.                     vector[i-restar] = vector[i];
  128.                 }
  129.                 ppio = ppio-restar;
  130.                 tope = tope-restar;
  131.               }
  132.             }
  133.         }
  134.  
  135.  
  136.     private:
  137.         int tope;
  138.         int ppio;
  139.  
  140.         TipoDato vector[300];
  141. };
  142.  
  143. #endif // COLA_H
  144.  
  145. /* fin de colagenerica.h */
  146.  
  147.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement