Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef PRIORITYQUEUE_H
- #define PRIORITYQUEUE_H
- #include <cstdlib>
- #include <iostream>
- #include <string>
- #include <sstream>
- using namespace std;
- /*
- Voorbeeld:
- PrioQueue<int> queue1;
- PrioQueue<int> queue2;
- PrioQueue<int> *queue3;
- queue1.insert(14);
- queue1.insert(10);
- queue1.insert(22);
- queue2.insert(-1);
- queue2.insert(30);
- queue2.insert(3);
- queue2.insert(2);
- // queue3 bestaat nu uit de items van zowel queue1 en queue2
- queue3 = queue1 + queue2;
- // Print de inhoud van queue3 op het scherm.
- cout << queue3 << endl;
- // Iterate over de queue:
- while(queue1.hasMore()) {
- cout << queue1.pop() << " ";
- }
- cout << endl;
- */
- template <class T>
- class Container {
- public:
- Container(){ next = NULL; previous = NULL; }
- ~Container(){ delete next; delete previous; }
- Container *next;
- Container *previous;
- T data;
- void insert(Container<T> *item, Container<T> *prev = NULL) {
- if(this->data < item->data) {
- if(prev == NULL) {
- T tmp = this->data;
- this->data = item->data;
- item->next = this->next;
- item->data = tmp;
- this->next = item;
- } else {
- item->next = this;
- prev->next = item;
- }
- } else {
- if(this->next == NULL) {
- this->next = item;
- } else {
- this->next->insert(item, this);
- }
- }
- }
- bool contains(const T &item) {
- if(data == item) {
- return true;
- }
- return (next != NULL) ? next->contains(item) : false;
- }
- int calculateDepth() {
- return (next != NULL) ? 1 + next->calculateDepth() : 0;
- }
- int byteSize() {
- return (next != NULL) ? sizeof(this) + next->byteSize() : sizeof(this);
- }
- };
- template <class T>
- class PrioQueue {
- private:
- Container<T>* head;
- public:
- PrioQueue(void) {
- head = NULL;
- }
- ~PrioQueue(void) {
- delete head;
- }
- void insert(T const &item) {
- Container<T> *newContainer = new Container<T>();
- newContainer->data = item;
- if(head == NULL) {
- head = newContainer;
- } else {
- if(newContainer->data > head->data) {
- newContainer->next = head;
- head = newContainer;
- } else {
- head->insert(newContainer);
- }
- }
- }
- bool contains(const T &data) {
- return (head != NULL) ? head->contains(data) : false;
- }
- T pop() {
- T data = head->data;
- head = head->next;
- return data;
- }
- T peek() {
- return head->data;
- }
- // This probably doesnt work :|
- int byteSize() {
- return (head == NULL) ? 0:head->byteSize();
- }
- // Expensive count, best way would be to keep a separate counter and de/in-crement whenever items are added/removed.
- int count() { // size? length? numElements? pick your name.
- return (head != NULL) ? 1 + head->calculateDepth() : 0;
- }
- string toString() {
- stringstream ss;
- Container<T> *iterator = head;
- while(iterator != NULL) {
- ss << iterator->data << "|";
- iterator = iterator->next;
- }
- return (ss.str().length() <= 1) ? "NILL":ss.str().substr(0, ss.str().length()-1);
- }
- bool hasMore() {
- return (head == NULL) ? false:true;
- }
- // Intersection operator. Items contained within both collections are returned.
- PrioQueue<T>* operator * (PrioQueue<T> &queue2) {
- PrioQueue<T> *tmp = new PrioQueue<T>();
- Container<T> *iterator = head;
- while(iterator != NULL) {
- if(queue2.contains(iterator->data)) {
- tmp->insert(iterator->data);
- }
- iterator = iterator->next;
- }
- return tmp;
- }
- // Adding operator, returns a new que with both queues combined.
- PrioQueue<T>* operator + (const PrioQueue<T> &queue2) const {
- PrioQueue<T> *tmp = new PrioQueue<T>();
- Container<T> *iterator = head;
- while(iterator != NULL) {
- tmp->insert(iterator->data);
- iterator = iterator->next;
- }
- iterator = queue2.head;
- while(iterator != NULL) {
- tmp->insert(iterator->data);
- iterator = iterator->next;
- }
- return tmp;
- }
- friend std::ostream& operator << (std::ostream& output, PrioQueue<T> q){
- output << q.toString();
- return output;
- }
- friend std::ostream& operator << (std::ostream& output, PrioQueue<T> *q){
- output << q->toString();
- return output;
- }
- };
- #endif
Advertisement
Add Comment
Please, Sign In to add comment