Implement "n" queue in an array
Pattern: Implementation
Keep k queues in one array of n slots, each queue a linked list through a next array with its own front and rear. Free slots form another list. Enqueue takes the first free slot and links it after the queue's rear; dequeue unlinks the front and returns the slot to the free list.
The problem
Build KQueues(k, n) holding k queues in one array of n slots: enqueue(x, qn) adds x to queue qn and returns
true, or false when all slots are used; dequeue(qn) removes and returns the front of queue qn, or -1 if it is
empty. Queues are numbered from 0. The return values are this guide's own.
Think first
- How is this different from k stacks in an array?
- What does each queue need to remember?
- Where does a freed slot go?
Hints
Brute force, and why it fails
Give each queue a fixed block of n / k slots as a circular queue: O(1), but space in one block cannot help another queue.
The key insight
The same next array links slots within each queue (front to rear) and links the free slots. Enqueue moves a slot from the free list to a queue's rear; dequeue moves a front slot back.
Dry run
| free | 0 |
|---|
KQueues(3, 5): every slot free, chained 0 -> 1 -> 2 -> 3 -> 4.
The plan
enqueue(x, qn): if free == -1: return false
i = free; free = next[i]; arr[i] = x; next[i] = -1
if rear[qn] == -1: front[qn] = i else: next[rear[qn]] = i
rear[qn] = i; return true
dequeue(qn): if front[qn] == -1: return -1
i = front[qn]; front[qn] = next[i]
if front[qn] == -1: rear[qn] = -1
next[i] = free; free = i; return arr[i]
Complexity
- Time
O(1) per operation- Space
O(n + k)
Each operation relinks a couple of slots.
Edge cases
- A queue that empties: its rear must reset to
-1. - All slots used by one queue.
- n = 1.
I've built my logic, show the code
Java
import java.util.*;
class KQueues {
private final int[] arr, next, front, rear; // next[i]: the slot after i in its queue, or the next free slot
private int free = 0;
public KQueues(int k, int n) {
arr = new int[n];
next = new int[n];
front = new int[k];
rear = new int[k];
Arrays.fill(front, -1);
Arrays.fill(rear, -1);
for (int i = 0; i < n - 1; i++) next[i] = i + 1; // every slot starts on the free list
next[n - 1] = -1;
}
public boolean enqueue(int x, int qn) {
if (free == -1) return false; // no free slot anywhere
int i = free;
free = next[i];
arr[i] = x;
next[i] = -1; // it will be the last in its queue
if (rear[qn] == -1) front[qn] = i; // the queue was empty
else next[rear[qn]] = i; // link after the old rear
rear[qn] = i;
return true;
}
public int dequeue(int qn) {
if (front[qn] == -1) return -1;
int i = front[qn];
front[qn] = next[i];
if (front[qn] == -1) rear[qn] = -1; // the queue became empty
next[i] = free; // give the slot back
free = i;
return arr[i];
}
}C++
class KQueues {
public:
vector<int> arr, nxt, frontOf, rearOf; // nxt[i]: the slot after i in its queue, or the next free slot
int freeSlot = 0;
KQueues(int k, int n) : arr(n), nxt(n), frontOf(k, -1), rearOf(k, -1) {
for (int i = 0; i < n - 1; i++) nxt[i] = i + 1; // every slot starts on the free list
nxt[n - 1] = -1;
}
bool enqueue(int x, int qn) {
if (freeSlot == -1) return false; // no free slot anywhere
int i = freeSlot;
freeSlot = nxt[i];
arr[i] = x;
nxt[i] = -1; // it will be the last in its queue
if (rearOf[qn] == -1) frontOf[qn] = i; // the queue was empty
else nxt[rearOf[qn]] = i; // link after the old rear
rearOf[qn] = i;
return true;
}
int dequeue(int qn) {
if (frontOf[qn] == -1) return -1;
int i = frontOf[qn];
frontOf[qn] = nxt[i];
if (frontOf[qn] == -1) rearOf[qn] = -1; // the queue became empty
nxt[i] = freeSlot; // give the slot back
freeSlot = i;
return arr[i];
}
};Python
class KQueues:
def __init__(self, k: int, n: int):
self.arr = [0] * n
self.next = list(range(1, n)) + [-1] # the slot after i in its queue, or the next free slot
self.front = [-1] * k
self.rear = [-1] * k
self.free = 0
def enqueue(self, x: int, qn: int) -> bool:
if self.free == -1:
return False # no free slot anywhere
i = self.free
self.free = self.next[i]
self.arr[i] = x
self.next[i] = -1 # it will be the last in its queue
if self.rear[qn] == -1:
self.front[qn] = i # the queue was empty
else:
self.next[self.rear[qn]] = i # link after the old rear
self.rear[qn] = i
return True
def dequeue(self, qn: int) -> int:
if self.front[qn] == -1:
return -1
i = self.front[qn]
self.front[qn] = self.next[i]
if self.front[qn] == -1:
self.rear[qn] = -1 # the queue became empty
self.next[i] = self.free # give the slot back
self.free = i
return self.arr[i]Common mistakes
- Forgetting to set
next[i] = -1for a new rear. - Not resetting
rearwhen the queue empties, so the next enqueue links onto a freed slot.