Advertisement
danielperezunlpam

FilaA

May 8th, 2019
711
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.98 KB | None | 0 0
  1. #ifndef FILAA_H
  2. #define FILAA_H
  3.  
  4. #include <cstdlib>
  5. #include <cstdio>
  6. #include <iostream>
  7. #include <iomanip>
  8.  
  9. #ifndef EXIT_ERROR
  10. #define EXIT_ERROR 254
  11. #endif
  12.  
  13. /**
  14.  * Implementación parametrizable del TDA Fila Circular.
  15.  * Estructura de datos lineal de tipo FIFO con
  16.  * complejidad temporal O(1) para todas las operaciones.
  17.  */
  18. template <class T, unsigned int LONG_MAX_FILA=15>
  19. class FilaA {
  20. public:
  21.     FilaA() {
  22.         ultimo = 0;
  23.         primero = 1;
  24.         cant = 0;
  25.     }
  26.  
  27.     int estaVacia() {
  28.         return primero == (ultimo+1)%LONG_MAX_FILA;
  29.     }
  30.  
  31.     void poner(T x) {
  32.         if (((((ultimo+1)%LONG_MAX_FILA)+1)%LONG_MAX_FILA) == primero) {
  33.             std::cerr << "Ha intentado poner en una fila llena";
  34.             exit(EXIT_ERROR);
  35.         } else {
  36.             ultimo = (ultimo+1)%LONG_MAX_FILA;
  37.             elementos[ultimo] = x;
  38.             cant++;
  39.         }
  40.     }
  41.  
  42.     void quitar() {
  43.         if (primero != (ultimo+1)%LONG_MAX_FILA) {
  44.             primero = (primero+1)%LONG_MAX_FILA;
  45.             cant--;
  46.         } else {
  47.             std::cerr << "Ha intentado retirar de una fila vacia";
  48.             exit(EXIT_ERROR);
  49.         }
  50.     }
  51.  
  52.     T recuperarFrente() {
  53.         if (primero == (ultimo+1)%LONG_MAX_FILA) {
  54.             std::cerr << "Ha intentado obtener el frente de una fila vacia";
  55.             exit(EXIT_ERROR);
  56.         }
  57.         return elementos[primero];
  58.     }
  59.  
  60.     int cantidad() {
  61.         return cant;
  62.     }
  63.  
  64.     void vaciar() {
  65.         ultimo = 0;
  66.         primero = 1;
  67.         cant = 0;
  68.     }
  69.  
  70. private:
  71.     T elementos[LONG_MAX_FILA];
  72.     unsigned int ultimo, primero;
  73.     unsigned int cant;
  74. };
  75.  
  76.  
  77. void test_fila() {
  78.  
  79.     std::cout << std::endl << __func__ << std::endl;
  80.     FilaA<int> numeros;
  81.     std::cout << "La fila posee " << numeros.cantidad()<< " elementos." << std::endl;
  82.     /////////////////////////////////////////////////////////
  83.     for (auto i : {1,2,3,4,5,6,7,8,9,10})
  84.         numeros.poner(i);
  85. //    while(true)
  86. //        numeros.poner(10001);
  87.     /////////////////////////////////////////////////////////
  88.     std::cout << "El elemento que se encuentra en el frente es " <<
  89.               numeros.recuperarFrente() << "." << std::endl;
  90.     if (!numeros.estaVacia())
  91.         numeros.quitar();
  92.     std::cout << "El elemento que se encuentra en el frente es " <<
  93.               numeros.recuperarFrente() << "." << std::endl;
  94.     std::cout << "La fila posee " << numeros.cantidad() << " elementos"
  95.               << "." << std::endl;
  96.     /////////////////////////////////////////////////////////
  97.     std::cout << "Elementos:"  << std::endl;
  98.     while (!numeros.estaVacia()) {
  99.         std::cout << std::setw(3) << std::setfill('0') <<
  100.                   numeros.recuperarFrente() << std::endl;
  101.         numeros.quitar();
  102.     }
  103.     /////////////////////////////////////////////////////////
  104.     std::cout << "La fila posee " << numeros.cantidad() << " elementos." << std::endl;
  105.     std::cout << "La fila esta vacia " <<(numeros.estaVacia()?"Si":"No");
  106.     std::cout << std::endl << std::endl;
  107.  
  108.     /////////////////////////////////////////////////////////
  109.     /////////////////////////////////////////////////////////
  110.     std::cout << std::endl << __func__ << " string" << std::endl<< std::endl;
  111.     FilaA<std::string, 100> stringQueue;
  112.     std::string s = "\
  113. Queues are a type of container adaptor, specifically designed to operate \
  114. in a FIFO context (first-in first-out), where elements are \
  115. inserted into one end of the container and extracted from the other.";
  116.     std::string delimiter = " ";
  117.     size_t pos = 0;
  118.     std::string token;
  119.     while ((pos = s.find(delimiter)) != std::string::npos) {
  120.         token = s.substr(0, pos);
  121.         stringQueue.poner(token);
  122.         s.erase(0, pos + delimiter.length());
  123.     }
  124.     stringQueue.poner(s);
  125.     while (!stringQueue.estaVacia()) {
  126.         std::cout << stringQueue.recuperarFrente() << std::endl;
  127.         stringQueue.quitar();
  128.     }
  129. }
  130. #endif  // FILAA_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement