Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* main.cpp */
- #include <iostream>
- #include "heap.h"
- using namespace std;
- class Paciente {
- public:
- Paciente() {
- _urgencia = 0;
- _edad = 0;
- _nombre = "";
- };
- Paciente(int urgencia, int edad, string nombre) {
- _urgencia = urgencia;
- _edad = edad;
- _nombre = nombre;
- }
- void setUrgencia(int valor) {
- _urgencia = valor;
- }
- int getUrgencia() {
- return _urgencia;
- }
- void setEdad(int valor) {
- _edad = valor;
- }
- int getEdad() {
- return _edad;
- }
- void setNombre(int valor) {
- _nombre = valor;
- }
- string getNombre() {
- return _nombre;
- }
- private:
- int _urgencia;
- int _edad;
- string _nombre;
- };
- int main() {
- /** ORDENAR CON UN HEAP **/
- int valor;
- Heap<int> heap_de_enteros;
- heap_de_enteros.agregar(9,9); // (valor, prioridad)
- heap_de_enteros.agregar(4,4);
- heap_de_enteros.agregar(8,8);
- heap_de_enteros.agregar(17,17);
- heap_de_enteros.agregar(2,2);
- heap_de_enteros.agregar(3,3);
- while (!heap_de_enteros.vacio()) {
- valor = heap_de_enteros.extraer();
- cout << endl << valor << endl;
- }
- /**CON CLASES***/
- Heap<Paciente> heap_de_pacientes;
- // urgencia, edad, nombre
- Paciente p1(4,37, "David");
- Paciente p2(6,27, "Maria");
- Paciente p3(5,17, "Juan");
- Paciente p4(1, 7, "Rosa");
- Paciente p5(5,57, "Roberto");
- // agregar (elemento, su_prioridad)
- heap_de_pacientes.agregar(p1, p1.getUrgencia());
- heap_de_pacientes.agregar(p2, p2.getUrgencia());
- heap_de_pacientes.agregar(p3, p3.getUrgencia());
- heap_de_pacientes.agregar(p4, p4.getUrgencia());
- heap_de_pacientes.agregar(p5, p5.getUrgencia());
- while (!heap_de_pacientes..vacio()) {
- Paciente paciente = heap_de_pacientes.extraer();
- cout << endl << " Paciente";
- cout << endl << " Nombre:" << paciente.getNombre();
- cout << endl << " Edad:" << paciente.getEdad();
- cout << endl << " Urgencia:" << paciente.getUrgencia();
- cout << endl;
- }
- return 0;
- }
- /* fin de main.cpp */
- /* heap.h */
- #ifndef HEAP_H
- #define HEAP_H
- #include <queue>
- template <class TipoDato>
- class Heap {
- public:
- Heap() {};
- virtual ~Heap() {};
- void agregar(TipoDato elemento, int prioridad) {
- ParPrioridadDato par;
- par.first = prioridad;
- par.second= elemento;
- h2.push(par);
- };
- TipoDato extraer(){
- if(!h2.empty()) {
- ParPrioridadDato par = h2.top();
- h2.pop();
- return par.second;
- } else {
- TipoDato datoNull;
- return datoNull;
- }
- }
- bool vacio() {
- return h2.empty();
- }
- private:
- typedef std::pair<int, TipoDato> ParPrioridadDato; // Prioridad, orden
- class ComparePrioridad {
- public:
- bool operator() (ParPrioridadDato a, ParPrioridadDato b) {
- return a.first > b.first;
- }
- };
- std::priority_queue<ParPrioridadDato, std::vector<ParPrioridadDato>, ComparePrioridad> h2;
- };
- #endif // HEAP_H
- /* FIN DE heap.h */
- /* heap.cpp */
- #include "heap.h"
- /* FIN DE heap.cpp */
Add Comment
Please, Sign In to add comment