fabgonber

heap con clases

Nov 4th, 2020 (edited)
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.27 KB | None | 0 0
  1. /* main.cpp */
  2. #include <iostream>
  3. #include "heap.h"
  4.  
  5. using namespace std;
  6.  
  7.  
  8. class Paciente {
  9. public:
  10.     Paciente() {
  11.         _urgencia = 0;
  12.         _edad = 0;
  13.         _nombre = "";
  14.     };
  15.  
  16.     Paciente(int urgencia, int edad, string nombre) {
  17.         _urgencia = urgencia;
  18.         _edad = edad;
  19.         _nombre = nombre;
  20.     }
  21.  
  22.     void setUrgencia(int valor) {
  23.         _urgencia = valor;
  24.     }
  25.  
  26.     int getUrgencia() {
  27.         return _urgencia;
  28.     }
  29.  
  30.     void setEdad(int valor) {
  31.         _edad = valor;
  32.     }
  33.     int getEdad() {
  34.         return _edad;
  35.     }
  36.  
  37.     void setNombre(int valor) {
  38.         _nombre = valor;
  39.     }
  40.  
  41.     string getNombre() {
  42.         return _nombre;
  43.     }
  44.  
  45. private:
  46.     int _urgencia;
  47.     int _edad;
  48.     string _nombre;
  49. };
  50.  
  51. int main() {
  52.  
  53.     /** ORDENAR CON UN HEAP **/
  54.     int valor;
  55.     Heap<int> heap_de_enteros;
  56.  
  57.     heap_de_enteros.agregar(9,9); // (valor, prioridad)
  58.     heap_de_enteros.agregar(4,4);
  59.     heap_de_enteros.agregar(8,8);
  60.     heap_de_enteros.agregar(17,17);
  61.     heap_de_enteros.agregar(2,2);  
  62.     heap_de_enteros.agregar(3,3);          
  63.  
  64.     while (!heap_de_enteros.vacio()) {
  65.         valor = heap_de_enteros.extraer();
  66.         cout << endl << valor << endl;
  67.     }
  68.    
  69.  
  70.  
  71.     /**CON CLASES***/
  72.     Heap<Paciente> heap_de_pacientes;
  73.                 // urgencia, edad, nombre
  74.     Paciente p1(4,37, "David");
  75.     Paciente p2(6,27, "Maria");
  76.     Paciente p3(5,17, "Juan");
  77.     Paciente p4(1, 7, "Rosa");
  78.     Paciente p5(5,57, "Roberto");
  79.  
  80.     // agregar (elemento, su_prioridad)
  81.     heap_de_pacientes.agregar(p1, p1.getUrgencia());
  82.     heap_de_pacientes.agregar(p2, p2.getUrgencia());
  83.     heap_de_pacientes.agregar(p3, p3.getUrgencia());
  84.     heap_de_pacientes.agregar(p4, p4.getUrgencia());
  85.     heap_de_pacientes.agregar(p5, p5.getUrgencia());
  86.  
  87.  
  88.     while (!heap_de_pacientes..vacio()) {
  89.         Paciente paciente = heap_de_pacientes.extraer();
  90.         cout << endl << " Paciente";
  91.         cout << endl << " Nombre:"   << paciente.getNombre();
  92.         cout << endl << " Edad:"     << paciente.getEdad();
  93.         cout << endl << " Urgencia:" << paciente.getUrgencia();
  94.         cout << endl;
  95.     }
  96.  
  97.     return 0;
  98. }
  99.  
  100. /* fin de main.cpp */
  101.  
  102.  
  103. /* heap.h */
  104. #ifndef HEAP_H
  105. #define HEAP_H
  106. #include <queue>
  107. template <class TipoDato>
  108. class Heap {
  109. public:
  110.     Heap() {};
  111.     virtual ~Heap() {};
  112.  
  113.     void agregar(TipoDato elemento, int prioridad) {
  114.         ParPrioridadDato par;
  115.         par.first = prioridad;
  116.         par.second= elemento;
  117.         h2.push(par);
  118.     };
  119.  
  120.     TipoDato extraer(){
  121.         if(!h2.empty()) {
  122.             ParPrioridadDato par = h2.top();
  123.             h2.pop();
  124.             return par.second;
  125.         } else {
  126.             TipoDato datoNull;
  127.             return datoNull;
  128.         }
  129.     }
  130.  
  131.     bool vacio() {
  132.         return h2.empty();
  133.     }
  134.  
  135. private:
  136.     typedef std::pair<int, TipoDato> ParPrioridadDato; // Prioridad, orden
  137.  
  138.     class ComparePrioridad {
  139.         public:
  140.             bool operator() (ParPrioridadDato a, ParPrioridadDato b) {
  141.                 return a.first > b.first;
  142.             }
  143.     };
  144.     std::priority_queue<ParPrioridadDato, std::vector<ParPrioridadDato>, ComparePrioridad> h2;
  145. };
  146.  
  147. #endif // HEAP_H
  148. /* FIN DE heap.h */
  149.  
  150.  
  151. /* heap.cpp */
  152. #include "heap.h"
  153. /* FIN DE heap.cpp */
Add Comment
Please, Sign In to add comment