Gerard-Meier

PrioQueue / linked list

May 19th, 2011
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.26 KB | None | 0 0
  1. #ifndef PRIORITYQUEUE_H
  2. #define PRIORITYQUEUE_H
  3.  
  4. #include <cstdlib>
  5. #include <iostream>
  6. #include <string>
  7. #include <sstream>
  8.  
  9. using namespace std;
  10.  
  11. /*
  12. Voorbeeld:
  13.  
  14.     PrioQueue<int> queue1;
  15.     PrioQueue<int> queue2;
  16.     PrioQueue<int> *queue3;
  17.     queue1.insert(14);
  18.     queue1.insert(10);
  19.     queue1.insert(22);
  20.  
  21.     queue2.insert(-1);
  22.     queue2.insert(30);
  23.     queue2.insert(3);
  24.     queue2.insert(2);
  25.  
  26.     // queue3 bestaat nu uit de items van zowel queue1 en queue2
  27.     queue3 = queue1 + queue2;
  28.  
  29.     // Print de inhoud van queue3 op het scherm.
  30.     cout << queue3 << endl;
  31.  
  32.     // Iterate over de queue:
  33.     while(queue1.hasMore()) {
  34.         cout << queue1.pop() << " ";
  35.     }
  36.  
  37.     cout << endl;
  38.  
  39. */
  40.  
  41.  
  42. template <class T>
  43. class Container {
  44.     public:
  45.         Container(){ next = NULL; previous = NULL; }
  46.         ~Container(){ delete next; delete previous; }
  47.  
  48.         Container *next;
  49.         Container *previous;
  50.         T data;
  51.  
  52.         void insert(Container<T> *item, Container<T> *prev = NULL) {
  53.  
  54.             if(this->data < item->data) {
  55.                 if(prev == NULL) {
  56.                     T tmp       = this->data;
  57.                     this->data = item->data;
  58.                     item->next = this->next;
  59.                     item->data = tmp;
  60.                     this->next = item;
  61.                        
  62.                 } else {
  63.                     item->next = this;
  64.                     prev->next = item;
  65.                        
  66.                 }
  67.             } else {
  68.                 if(this->next == NULL) {
  69.                     this->next = item;
  70.                 } else {
  71.                     this->next->insert(item, this);
  72.                 }
  73.             }
  74.            
  75.         }
  76.  
  77.         bool contains(const T &item) {
  78.             if(data == item) {
  79.                 return true;
  80.             }
  81.             return (next != NULL) ? next->contains(item) : false;
  82.         }
  83.  
  84.         int calculateDepth() {
  85.             return (next != NULL) ? 1 + next->calculateDepth() : 0;
  86.         }
  87.  
  88.         int byteSize() {
  89.             return (next != NULL) ? sizeof(this) + next->byteSize() : sizeof(this);
  90.         }
  91. };
  92.  
  93.  
  94. template <class T>
  95. class PrioQueue {
  96.     private:
  97.         Container<T>* head;
  98.  
  99.     public:
  100.         PrioQueue(void) {
  101.             head = NULL;
  102.         }
  103.         ~PrioQueue(void) {
  104.             delete head;
  105.         }
  106.  
  107.         void insert(T const &item) {
  108.             Container<T> *newContainer = new Container<T>();
  109.             newContainer->data = item;
  110.        
  111.             if(head == NULL) {
  112.                 head = newContainer;
  113.             } else {
  114.                 if(newContainer->data > head->data) {
  115.                     newContainer->next = head;
  116.                     head = newContainer;
  117.                 } else {
  118.                     head->insert(newContainer);
  119.                 }
  120.             }
  121.         }
  122.  
  123.         bool contains(const T &data) {
  124.             return (head != NULL) ? head->contains(data) : false;
  125.         }
  126.  
  127.         T pop() {
  128.             T data  = head->data;
  129.             head    = head->next;
  130.             return data;
  131.         }
  132.  
  133.         T peek() {
  134.             return head->data;
  135.         }
  136.  
  137.         // This probably doesnt work :|
  138.         int byteSize() {
  139.             return (head == NULL) ? 0:head->byteSize();
  140.         }
  141.        
  142.         // Expensive count, best way would be to keep a separate counter and de/in-crement whenever items are added/removed.
  143.         int count() { // size? length? numElements? pick your name.
  144.             return (head != NULL) ? 1 + head->calculateDepth() : 0;
  145.         }
  146.  
  147.         string toString() {
  148.             stringstream ss;
  149.             Container<T> *iterator = head;
  150.             while(iterator != NULL) {
  151.                 ss << iterator->data << "|";
  152.                 iterator = iterator->next;
  153.             }
  154.            
  155.            
  156.             return (ss.str().length() <= 1) ? "NILL":ss.str().substr(0, ss.str().length()-1);
  157.         }
  158.  
  159.         bool hasMore() {
  160.             return (head == NULL) ? false:true;
  161.         }
  162.  
  163.        
  164.         // Intersection operator. Items contained within both collections are returned.
  165.         PrioQueue<T>* operator * (PrioQueue<T> &queue2) {
  166.             PrioQueue<T> *tmp = new PrioQueue<T>();
  167.  
  168.             Container<T> *iterator = head;
  169.             while(iterator != NULL) {
  170.                 if(queue2.contains(iterator->data)) {
  171.                     tmp->insert(iterator->data);
  172.                 }
  173.                 iterator = iterator->next;
  174.             }
  175.  
  176.             return tmp;
  177.         }
  178.    
  179.         // Adding operator, returns a new que with both queues combined.
  180.         PrioQueue<T>* operator + (const PrioQueue<T> &queue2) const {
  181.             PrioQueue<T> *tmp = new PrioQueue<T>();
  182.            
  183.             Container<T> *iterator = head;
  184.             while(iterator != NULL) {
  185.                 tmp->insert(iterator->data);
  186.  
  187.                 iterator = iterator->next;
  188.             }
  189.  
  190.             iterator = queue2.head;
  191.             while(iterator != NULL) {
  192.                 tmp->insert(iterator->data);
  193.  
  194.                 iterator = iterator->next;
  195.             }
  196.            
  197.             return tmp;
  198.         }
  199.  
  200.         friend std::ostream& operator << (std::ostream& output, PrioQueue<T> q){
  201.             output << q.toString();
  202.             return output;
  203.         }
  204.  
  205.         friend std::ostream& operator << (std::ostream& output, PrioQueue<T> *q){
  206.             output << q->toString();
  207.             return output;
  208.         }
  209. };
  210.  
  211. #endif
Advertisement
Add Comment
Please, Sign In to add comment