Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <iostream>
- using namespace std;
- template <class T>
- struct Bug {
- char m_cpriority;
- string m_sDesc;
- T data;
- Bug(const T &);
- Bug(const T &, char );
- char getPriority() {
- return this.m_cpriority;
- }
- };
- template <class T>
- class Node
- {
- public:
- T m_tData;
- Node<T>* m_pNext;
- Node(T val) : m_tData(val), m_pNext(nullptr) {}
- };
- template <class T>
- class PriorityQueue {
- private:
- Node<T> *head;
- public:
- PriorityQueue();
- ~PriorityQueue();
- void enQueue(const Bug<T> &);
- void enQueue(T val, char m_cpriority);
- Bug<T> deQueue();
- bool isEmpty() const;
- void print() const;
- };
- template <class T>
- class BasicQueue
- {
- private:
- Node<T>* m_pHead;
- Node<T>* m_cpriority;
- public:
- BasicQueue() : m_pHead(nullptr), m_cpriority(nullptr) {}
- ~BasicQueue()
- {
- //Node<T>* current = m_pHead; // Consider this an iterator.
- Node<T>* next;
- while (m_pHead != nullptr)
- {
- next = m_pHead->m_pNext;
- delete m_pHead;
- m_pHead = next;
- }
- }
- void enQueue(T val) // 'Push' method.
- {
- Node<T>* newNode = new Node<T>(val);
- if (m_pHead == nullptr) // List is empty.
- {
- m_pHead = m_cpriority = newNode;
- }
- else // New node becomes the new tail.
- {
- m_cpriority->m_pNext = newNode;
- m_cpriority = m_cpriority->m_pNext;
- }
- }
- T deQueue() // 'Pop' method.
- {
- if (m_pHead != nullptr)
- {
- T val = m_pHead->m_tData; // Storing the data to be returned.
- Node<T>* next;
- next = m_pHead->m_pNext;
- delete m_pHead; // Deallocating head.
- m_pHead = next;
- return val; // Throwing copy of old head's data.
- }
- }
- bool isEmpty()
- {
- return m_pHead == nullptr;
- }
- void print()
- {
- if (isEmpty())
- return;
- cout << "Queue contents: ";
- Node<T>* current = m_pHead;
- while (current != nullptr)
- {
- cout << current->m_tData << ' ';
- current = current->m_pNext;
- }
- cout << endl;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement