Advertisement
xSiRON

Priority Queue

Jun 25th, 2016
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.24 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. template <class H> class Nodo{
  5.     H* key;
  6.     Nodo<H> *next, *prev;
  7.  
  8. public:
  9.     Nodo(H* key = NULL){
  10.         this->key = NULL;
  11.         if(key) this->key = new H(*key);
  12.         next = prev = NULL;
  13.     }
  14.  
  15.     Nodo<H>* getPrev(){ return prev; }
  16.     Nodo<H>* getNext(){ return next; }
  17.     H* getKey(){ return key; }
  18.  
  19.     void setPrev(Nodo<H>* prev){ this->prev = prev; }
  20.     void setNext(Nodo<H>* next){ this->next = next; }
  21.     void setKey(H* key){ if(key) this->key = new H(*key); }
  22. };
  23.  
  24. template <class H> class List{
  25.  
  26. protected:
  27.     Nodo<H> *header, *trailer, *current;
  28.     int n;
  29.  
  30.     Nodo<H>* _search(H x){
  31.         Nodo<H>* tmp = header->getNext();
  32.         while(tmp != trailer && *tmp->getKey() != x)
  33.             tmp = tmp->getNext();
  34.         return tmp;
  35.     }
  36.  
  37. public:
  38.     List(){
  39.         header = new Nodo<H>();
  40.         trailer = new Nodo<H>();
  41.         header->setNext(trailer);
  42.         trailer->setPrev(header);
  43.         n = 0;
  44.     }  
  45.  
  46.     void insert(H x){
  47.         Nodo<H> *nd = new Nodo<H>(&x);
  48.  
  49.         n++;
  50.  
  51.         Nodo<H>* tmp = header->getNext();
  52.         if(tmp == trailer){
  53.             nd->setNext(trailer);
  54.             nd->setPrev(header);
  55.             header->setNext(nd);
  56.             trailer->setPrev(nd);
  57.             return;
  58.         }
  59.  
  60.         while(tmp != trailer && *tmp->getKey() > *nd->getKey())
  61.             tmp = tmp->getNext();
  62.        
  63.         nd->setPrev(tmp->getPrev());
  64.         nd->setNext(tmp);
  65.         tmp->getPrev()->setNext(nd);
  66.         tmp->setPrev(nd);
  67.     }
  68.  
  69.     void del(H x){
  70.         Nodo<H>* tmp = _search(x);
  71.         if(tmp != trailer){
  72.             tmp->getPrev()->setNext(tmp->getNext());
  73.             tmp->getNext()->setPrev(tmp->getPrev());
  74.             delete tmp;
  75.             n--;
  76.         }
  77.     }
  78.  
  79.     H* begin(){
  80.         current = header;
  81.         return next();
  82.     }
  83.  
  84.     H* next(){
  85.         if(current->getNext() == trailer) return NULL;
  86.         current = current->getNext();
  87.         return current->getKey();
  88.     }
  89.  
  90.     int isEmpty(){ return n == 0; }
  91. };
  92.  
  93. template <class H> class ListQueue: public List<H>{
  94.     int prior, n;
  95.  
  96. public:
  97.     ListQueue(int prior) : List<H>() {
  98.         this->prior = prior;
  99.         n = 0;
  100.     }
  101.  
  102.     ListQueue<H>* enqueue(H x){
  103.         Nodo<H>* nd = new Nodo<H>(&x);
  104.         nd->setNext(List<H>::trailer);
  105.         nd->setPrev(List<H>::trailer->getPrev());
  106.         nd->getNext()->setPrev(nd);
  107.         nd->getPrev()->setNext(nd);
  108.         n++;
  109.         return this;
  110.     }
  111.  
  112.     H* dequeue(){
  113.         Nodo<H>* tmp = List<H>::header->getNext();
  114.         if(tmp != List<H>::trailer){
  115.             H* val = tmp->getKey();
  116.             tmp->getNext()->setPrev(List<H>::header);
  117.             tmp->getPrev()->setNext(tmp->getNext());
  118.             delete tmp;
  119.             n--;
  120.             return val;
  121.         }
  122.         return NULL;
  123.     }
  124.  
  125.     int getPriority(){ return prior; }
  126. };
  127.  
  128. /************ OVERLOADING OPERATORI ***********/
  129.  
  130. int operator>(ListQueue<int>& a, ListQueue<int>& b){
  131.     if(!a.begin()) return 0;
  132.     if(!b.begin()) return 1;
  133.     return (a.getPriority() > b.getPriority()) ? 1 : 0;
  134. }
  135.  
  136. int operator!=(ListQueue<int>& a, ListQueue<int>& b){
  137.     if(!a.begin() || !b.begin()) return 1;
  138.     return (a.getPriority() != b.getPriority()) ? 1 : 0;
  139. }
  140.  
  141. template <class H> ostream& operator<<(ostream& out, ListQueue<H>& obj){
  142.     for(H* it = obj.begin(); it; it = obj.next())
  143.         out << *it << " ";
  144.     return out;
  145. }
  146.  
  147. /********* IMPLEMENTAZIONE PQUEUE *************/
  148.  
  149. template <class H> class PQueue {
  150. public:
  151.     virtual PQueue<H>* enqueue(H x, int p) = 0;
  152.     virtual H* dequeue() = 0;
  153.     virtual void print() = 0;
  154. };
  155.  
  156. template <class H> class MyPQueue: public PQueue<H>{
  157.     List< ListQueue<H> >* MPQ;
  158.     int n;
  159.  
  160. public:
  161.     MyPQueue(){
  162.         MPQ = new List< ListQueue<H> >();
  163.         n = 0;
  164.     }
  165.  
  166.     PQueue<H>* enqueue(H x, int p){
  167.         n++;
  168.  
  169.         if(!MPQ->begin()){
  170.             ListQueue<H>* tmp = new ListQueue<H>(p);
  171.             tmp->enqueue(x);
  172.             MPQ->insert(*tmp);
  173.             return this;
  174.         }
  175.  
  176.         ListQueue<H>* it = MPQ->begin();
  177.         while(it && it->getPriority() != p) it = MPQ->next();
  178.  
  179.         if(!it){
  180.             ListQueue<H>* tmp = new ListQueue<H>(p);
  181.             tmp->enqueue(x);
  182.             MPQ->insert(*tmp);
  183.         }else it->enqueue(x);
  184.         return this;
  185.     }
  186.  
  187.     H* dequeue(){
  188.         if(!MPQ->isEmpty()){
  189.             ListQueue<H>* tmp = MPQ->begin();
  190.             H* val = tmp->dequeue();
  191.             if(tmp->isEmpty()) MPQ->del(*tmp);
  192.             return val;
  193.         }
  194.         return NULL;
  195.     }
  196.  
  197.     void print(){
  198.         for(ListQueue<H>* it = MPQ->begin(); it; it = MPQ->next())
  199.             cout << *it << endl;
  200.         cout << endl;
  201.     }
  202. };
  203.  
  204. int main(){
  205.     MyPQueue<int>* MPQ = new MyPQueue<int>();
  206.     MPQ->enqueue(2, 8)->enqueue(4, 7)->print();
  207.     MPQ->enqueue(8, 7)->print();
  208.     MPQ->enqueue(13, 4)->enqueue(28, 3)->print();
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement