We have discussed efficient implementation of k stack in an array. In this post, same for queue is discussed. Following is the detailed problem statement.
Create a data structure kQueues that represents k queues. Implementation of kQueues should use only one array, i.e., k queues should use the same array for storing elements. Following functions must be supported by kQueues.
enqueue(int x, int qn) –> adds x to queue number ‘qn’ where qn is from 0 to k-1
dequeue(int qn) –> deletes an element from queue number ‘qn’ where qn is from 0 to k-1
Method 1 (Divide the array in slots of size n/k)
A simple way to implement k queues is to divide the array in k slots of size n/k each, and fix the slots for different queues, i.e., use arr to arr[n/k-1] for the first queue, and arr[n/k] to arr[2n/k-1] for queue2 where arr is the array to be used to implement two queues and size of array be n.
The problem with this method is an inefficient use of array space. An enqueue operation may result in overflow even if there is space available in arr. For example, consider k as 2 and array size n as 6. Let we enqueue 3 elements to first and do not enqueue anything to the second queue. When we enqueue the 4th element to the first queue, there will be overflow even if we have space for 3 more elements in the array.
Method 2 (A space efficient implementation)
The idea is similar to the stack post, here we need to use three extra arrays. In stack post, we needed two extra arrays, one more array is required because in queues, enqueue() and dequeue() operations are done at different ends.
Following are the three extra arrays are used:
1) front: This is of size k and stores indexes of front elements in all queues.
2) rear: This is of size k and stores indexes of rear elements in all queues.
2) next: This is of size n and stores indexes of next item for all items in array arr.
Here arr is the actual array that stores k stacks.
Together with k queues, a stack of free slots in arr is also maintained. The top of this stack is stored in a variable ‘free’.
All entries in front are initialized as -1 to indicate that all queues are empty. All entries next[i] are initialized as i+1 because all slots are free initially and pointing to the next slot. Top of the free stack, ‘free’ is initialized as 0.
Following is C++ implementation of the above idea.
Dequeued element from queue 2 is 15 Dequeued element from queue 1 is 17 Dequeued element from queue 0 is 11
Time complexities of enqueue() and dequeue() is O(1).
The best part of the above implementation is, if there is a slot available in the queue, then an item can be enqueued in any of the queues, i.e., no wastage of space. This method requires some extra space. Space may not be an issue because queue items are typically large, for example, queues of employees, students, etc where every item is of hundreds of bytes. For such large queues, the extra space used is comparatively very less as we use three integer arrays as extra space.
This article is contributed by Sachin. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
- Implement Stack using Queues
- Implement a stack using single queue
- Implement two stacks in an array
- Level order traversal line by line | Set 2 (Using Two Queues)
- Implement PriorityQueue through Comparator in Java
- Implement Stack and Queue using Deque
- Zig Zag Level order traversal of a tree using single queue
- Find an element in array such that sum of left array is equal to sum of right array
- Find whether an array is subset of another array | Added Method 3
- Segregate 0s and 1s in an array
- A Product Array Puzzle
- Array implementation of queue (Simple)
- A product array puzzle | Set 2 (O(1) Space)
- Queue | Set 1 (Introduction and Array Implementation)
- Find Second largest element in an array