• Source
    1. // A C++ program to demonstrate implementation of k queues in a single
    2. // array in time and space efficient way
    3. #include<iostream>
    4. #include<climits>
    5. using namespace std;
    6.  
    7. // A C++ class to represent k queues in a single array of size n
    8. class kQueues
    9. {
    10. int *arr; // Array of size n to store actual content to be stored in queue
    11. int *front; // Array of size k to store indexes of front elements of queue
    12. int *rear; // Array of size k to store indexes of rear elements of queue
    13. int *next; // Array of size n to store next entry in all queues
    14. // and free list
    15. int n, k;
    16. int free; // To store beginning index of free list
    17. public:
    18. //constructor to create k queue in an array of size n
    19. kQueues(int k, int n);
    20.  
    21. // A utility function to check if there is space available
    22. bool isFull() { return (free == -1); }
    23.  
    24. // To enqueue an item in queue number 'qn' where qn is from 0 to k-1
    25. void enqueue(int item, int qn);
    26.  
    27. // To dequeue an from queue number 'qn' where qn is from 0 to k-1
    28. int dequeue(int qn);
    29.  
    30. // To check whether queue number 'qn' is empty or not
    31. bool isEmpty(int qn) { return (front[qn] == -1); }
    32. };
    33.  
    34. // Constructor to create k queues in an array of size n
    35. kQueues::kQueues(int k1, int n1)
    36. {
    37. // Initialize n and k, and allocate memory for all arrays
    38. k = k1, n = n1;
    39. arr = new int[n];
    40. front = new int[k];
    41. rear = new int[k];
    42. next = new int[n];
    43.  
    44. // Initialize all queues as empty
    45. for (int i = 0; i < k; i++)
    46. front[i] = -1;
    47.  
    48. // Initialize all spaces as free
    49. free = 0;
    50. for (int i=0; i<n-1; i++)
    51. next[i] = i+1;
    52. next[n-1] = -1; // -1 is used to indicate end of free list
    53. }
    54.  
    55. // To enqueue an item in queue number 'qn' where qn is from 0 to k-1
    56. void kQueues::enqueue(int item, int qn)
    57. {
    58. // Overflow check
    59. if (isFull())
    60. {
    61. cout << "\nQueue Overflow\n";
    62. return;
    63. }
    64.  
    65. int i = free; // Store index of first free slot
    66.  
    67. // Update index of free slot to index of next slot in free list
    68. free = next[i];
    69.  
    70. if (isEmpty(qn))
    71. front[qn] = i;
    72. else
    73. next[rear[qn]] = i;
    74.  
    75. next[i] = -1;
    76.  
    77. // Update next of rear and then rear for queue number 'qn'
    78. rear[qn] = i;
    79.  
    80. // Put the item in array
    81. arr[i] = item;
    82. }
    83.  
    84. // To dequeue an from queue number 'qn' where qn is from 0 to k-1
    85. int kQueues::dequeue(int qn)
    86. {
    87. // Underflow checkSAS
    88. if (isEmpty(qn))
    89. {
    90. cout << "\nQueue Underflow\n";
    91. return INT_MAX;
    92. }
    93.  
    94. // Find index of front item in queue number 'qn'
    95. int i = front[qn];
    96.  
    97. front[qn] = next[i]; // Change top to store next of previous top
    98.  
    99. // Attach the previous front to the beginning of free list
    100. next[i] = free;
    101. free = i;
    102.  
    103. // Return the previous front item
    104. return arr[i];
    105. }
    106.  
    107. /* Driver program to test kStacks class */
    108. int main()
    109. {
    110. // Let us create 3 queue in an array of size 10
    111. int k = 3, n = 10;
    112. kQueues ks(k, n);
    113.  
    114. // Let us put some items in queue number 2
    115. ks.enqueue(15, 2);
    116. ks.enqueue(45, 2);
    117.  
    118. // Let us put some items in queue number 1
    119. ks.enqueue(17, 1);
    120. ks.enqueue(49, 1);
    121. ks.enqueue(39, 1);
    122.  
    123. // Let us put some items in queue number 0
    124. ks.enqueue(11, 0);
    125. ks.enqueue(9, 0);
    126. ks.enqueue(7, 0);
    127.  
    128. cout << "Dequeued element from queue 2 is " << ks.dequeue(2) << endl;
    129. cout << "Dequeued element from queue 1 is " << ks.dequeue(1) << endl;
    130. cout << "Dequeued element from queue 0 is " << ks.dequeue(0) << endl;
    131.  
    132. return 0;
    133. }