Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* INICIO DE main.cpp */
- #include <iostream>
- #include "colaint.h"
- #include "colagenerica.h"
- using namespace std;
- template<class TipoDato>
- void imprimir(Cola<TipoDato> & laCola)
- {
- while(!laCola.vacia())
- {
- cout << "Valor: " << laCola.extraer() << endl;
- }
- }
- void ejemplo_cuatro()
- {
- int i;
- cout << "Clase Cola Generica (implementacion estatica)" << endl;
- cout << "(usando plantilla de clases) " << endl << endl;
- Cola<int> unaCola;
- unaCola.agregar(1);
- unaCola.agregar(2);
- unaCola.agregar(3);
- cout << " e = " << unaCola.extraer() << endl;
- cout << "Vacio? " << unaCola.vacia() << endl;
- cout << " e = " << unaCola.extraer() << endl;
- cout << " e = " << unaCola.extraer() << endl;
- cout << "Vacio? " << unaCola.vacia() << endl << endl;
- for(i=1; i<7; i++)
- unaCola.agregar(i*2);
- imprimir(unaCola);
- imprimir(unaCola);
- cout << endl << " Ejemplo Cola Float " << endl;
- Cola<float> otra_cola;
- otra_cola.agregar(2.5);
- otra_cola.agregar(3.0);
- imprimir(otra_cola);
- cout << endl << " Ejemplo Cola strig " << endl;
- Cola<string> mi_cola;
- mi_cola.agregar("pedro");
- mi_cola.agregar("juan");
- imprimir(mi_cola);
- }
- int main() {
- cout << "\n=======================\n";
- ejemplo_cuatro();
- }
- /* FIN DE main.cpp */
- /* inicio de colagenerica.cpp */
- #include "colagenerica.h"
- /* fin de colagenerica.cpp */
- /* inicio de colagenerica.h */
- #ifndef COLAGENERICA_H
- #define COLAGENERICA_H
- template<class TipoDato>
- class Cola
- {
- public:
- Cola()
- {
- tope = 0;
- ppio = 0;
- }
- void agregar(TipoDato elemento)
- {
- vector[tope] = elemento;
- tope++;
- }
- TipoDato extraer()
- {
- TipoDato elemento;
- elemento = vector[ppio];
- ppio++;
- this->garbagecolector();
- return elemento;
- }
- bool vacia()
- {
- return (tope == ppio);
- }
- bool vacio()
- {
- return this->vacia();
- }
- protected:
- void garbagecolector()
- {
- int i, restar;
- if (tope == ppio)
- {
- tope = 0;
- ppio = 0;
- }
- else
- {
- if (ppio>50)
- {
- restar = ppio;
- for (i=ppio;i<tope;i++)
- {
- vector[i-restar] = vector[i];
- }
- ppio = ppio-restar;
- tope = tope-restar;
- }
- }
- }
- private:
- int tope;
- int ppio;
- TipoDato vector[300];
- };
- #endif // COLA_H
- /* fin de colagenerica.h */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement