Advertisement
Guest User

Untitled

a guest
Dec 15th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. #pragma once
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. template <class T>
  7. struct Bug {
  8. char m_cpriority;
  9. string m_sDesc;
  10. T data;
  11. Bug(const T &);
  12. Bug(const T &, char );
  13. char getPriority() {
  14. return this.m_cpriority;
  15. }
  16. };
  17.  
  18. template <class T>
  19. class Node
  20. {
  21. public:
  22. T m_tData;
  23. Node<T>* m_pNext;
  24. Node(T val) : m_tData(val), m_pNext(nullptr) {}
  25. };
  26.  
  27. template <class T>
  28. class PriorityQueue {
  29. private:
  30. Node<T> *head;
  31. public:
  32. PriorityQueue();
  33. ~PriorityQueue();
  34. void enQueue(const Bug<T> &);
  35. void enQueue(T val, char m_cpriority);
  36. Bug<T> deQueue();
  37. bool isEmpty() const;
  38. void print() const;
  39. };
  40.  
  41. template <class T>
  42. class BasicQueue
  43. {
  44. private:
  45. Node<T>* m_pHead;
  46. Node<T>* m_cpriority;
  47. public:
  48. BasicQueue() : m_pHead(nullptr), m_cpriority(nullptr) {}
  49. ~BasicQueue()
  50. {
  51. //Node<T>* current = m_pHead; // Consider this an iterator.
  52. Node<T>* next;
  53. while (m_pHead != nullptr)
  54. {
  55. next = m_pHead->m_pNext;
  56. delete m_pHead;
  57. m_pHead = next;
  58. }
  59. }
  60. void enQueue(T val) // 'Push' method.
  61. {
  62. Node<T>* newNode = new Node<T>(val);
  63. if (m_pHead == nullptr) // List is empty.
  64. {
  65. m_pHead = m_cpriority = newNode;
  66. }
  67. else // New node becomes the new tail.
  68. {
  69. m_cpriority->m_pNext = newNode;
  70. m_cpriority = m_cpriority->m_pNext;
  71. }
  72. }
  73. T deQueue() // 'Pop' method.
  74. {
  75. if (m_pHead != nullptr)
  76. {
  77. T val = m_pHead->m_tData; // Storing the data to be returned.
  78. Node<T>* next;
  79. next = m_pHead->m_pNext;
  80. delete m_pHead; // Deallocating head.
  81. m_pHead = next;
  82. return val; // Throwing copy of old head's data.
  83. }
  84. }
  85. bool isEmpty()
  86. {
  87. return m_pHead == nullptr;
  88. }
  89. void print()
  90. {
  91. if (isEmpty())
  92. return;
  93. cout << "Queue contents: ";
  94. Node<T>* current = m_pHead;
  95. while (current != nullptr)
  96. {
  97. cout << current->m_tData << ' ';
  98. current = current->m_pNext;
  99. }
  100. cout << endl;
  101. }
  102. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement