Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Queue {
  5. private: //private members can only be accessed in the class
  6. int queue_size;
  7. protected:
  8. int* buffer;
  9. int front;
  10. int rear;
  11. static int counter;
  12. public:
  13. Queue(void) {
  14. front = 0;
  15. rear = 0;
  16. queue_size = 10;
  17. buffer = new int[queue_size]; // create an array in the heap
  18. if (buffer != NULL) counter++;
  19. }
  20. Queue(int n) {
  21. front = 0;
  22. rear = 0;
  23. queue_size = n;
  24. buffer = new int[queue_size];
  25. if (buffer != NULL) counter++;
  26. }
  27. virtual ~Queue(void) {
  28. delete buffer;
  29. counter--;
  30. }
  31.  
  32. void enqueue(int v) { // add element at the end of queue
  33. if (rear < queue_size) buffer[rear++] = v;
  34. else if (compact()) buffer[rear++] = v;
  35. }
  36. int dequeue(void) { // return and remove 1st element from queue
  37. if (front < rear) return buffer[front++];
  38. else { cout << "Error:Queue empty" << endl; return -1; }
  39. }
  40. private:
  41. bool compact(void);
  42. };
  43. int Queue::counter = 0;
  44. //End of class definition
  45.  
  46. class PriQueue : public Queue {//PriQueue derived from Queue
  47. public:
  48. int getMax(void);
  49. PriQueue(int n) : Queue(n) {};
  50. ~PriQueue() {
  51. delete buffer;
  52. buffer = NULL;
  53. counter--;
  54. }
  55. };
  56.  
  57.  
  58. bool Queue::compact(void) {
  59. if (front == 0) {
  60. cout << "Error: Queue overflow" << endl;
  61. return false;
  62. }
  63. else {
  64. for (int i = 0; i < rear - front; i++)
  65. {
  66. buffer[i] = buffer[i + front];
  67. }
  68. rear = rear - front;
  69. front = 0;
  70. return true;
  71. }
  72. }
  73.  
  74. int PriQueue::getMax(void) {// get & remove max value from priority queue
  75. int i, max, imax;
  76. if (front < rear) {
  77. max = buffer[front];
  78. imax = front; // imax is index of current max value
  79. for (i = front; i < rear; i++) {
  80. if (max < buffer[i]) {
  81. max = buffer[i];
  82. imax = i;
  83. }
  84. }
  85. for (i = imax; i < rear - 1; i++)
  86. buffer[i] = buffer[i++]; // remove max value
  87. rear = rear - 1;
  88. return max;
  89. }
  90. else {
  91. cout << "Error: Queue empty" << endl;
  92. return -1;
  93. }
  94. }
  95.  
  96. //main outside all classes
  97. void main() {
  98. Queue Q1(5); // variable/object created using constructor in stack memory (note that buffer will create an array on the heap)
  99. Q1.enqueue(2);
  100. Q1.enqueue(8);
  101. int x = Q1.dequeue();
  102. int y = Q1.dequeue();
  103. cout << "x = " << x << endl << "y = " << y << endl;
  104.  
  105. Queue* Q2;
  106. Q2 = new Queue(4);
  107. Q2->enqueue(12); // insert 12
  108. Q2->enqueue(18); // insert 18
  109. x = Q2->dequeue();
  110. y = Q2->dequeue();
  111. cout << "x = " << x << endl << "y = " << y << endl;
  112.  
  113. PriQueue* Q3 = new PriQueue(4);
  114. Q3->enqueue(12);
  115. Q3->enqueue(18);
  116. Q3->enqueue(14);
  117. x = Q3->getMax();
  118. y = Q3->getMax();
  119. cout << "x = " << x << endl << "y = " << y << endl;
  120.  
  121.  
  122. delete Q2; Q2 = NULL;
  123. delete Q3; Q3 = NULL;
  124. system("pause");
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement