Implement Queue from Scratch
Pattern: Implementation
A queue with fixed capacity on an array, used as a ring. Keep head and count; the back slot is (head + count) mod capacity. Enqueue fails when count reaches the capacity; dequeue on an empty queue returns -1.
The problem
Build FixedQueue(capacity) with enqueue(x) returning true, or false when full, dequeue() returning the front or
-1 when empty, front() and rear() (each -1 when empty), isEmpty() and isFull().
The linked page builds the same array queue with front and rear indexes; this guide keeps head and count, which says full and empty apart with no extra rule.
Think first
- Why not shift items left after each dequeue?
- Where does the next item go?
- How do you tell full from empty?
Hints
Brute force, and why it fails
Shifting every item left after each dequeue costs O(n) per operation.
The key insight
Moving head forward with wrap-around reuses freed slots, so every operation is O(1).
Dry run
| count | 3 |
|---|
Capacity 3. enqueue 1, 2, 3 fill slots 0, 1, 2.
The plan
enqueue(x): if count == capacity: return false
data[(head + count) mod capacity] = x; count += 1; return true
dequeue(): if count == 0: return -1
x = data[head]; head = (head + 1) mod capacity; count -= 1; return x
front(): return -1 if count == 0 else data[head]
rear(): return -1 if count == 0 else data[(head + count - 1) mod capacity]
isEmpty(): count == 0; isFull(): count == capacity
Complexity
- Time
O(1) per operation- Space
O(capacity)
Each operation reads or writes one slot.
Edge cases
- Capacity 1.
- Many enqueue and dequeue rounds, so
headwraps. - Enqueue when full leaves the queue unchanged.
I've built my logic, show the code
Java
class FixedQueue {
private final int[] data;
private int head = 0, count = 0; // the front is data[head]; the rear is count - 1 places later, wrapping
public FixedQueue(int capacity) {
data = new int[capacity];
}
public boolean enqueue(int x) {
if (count == data.length) return false; // full
data[(head + count) % data.length] = x; // the slot after the rear, wrapping to the start
count++;
return true;
}
public int dequeue() {
if (count == 0) return -1; // empty
int x = data[head];
head = (head + 1) % data.length; // the front moves one slot, wrapping
count--;
return x;
}
public int front() {
return count == 0 ? -1 : data[head];
}
public int rear() {
return count == 0 ? -1 : data[(head + count - 1) % data.length];
}
public boolean isEmpty() {
return count == 0;
}
public boolean isFull() {
return count == data.length;
}
}C++
class FixedQueue {
public:
vector<int> data;
int head = 0, count = 0; // the front is data[head]; the rear is count - 1 places later, wrapping
FixedQueue(int capacity) : data(capacity) {}
bool enqueue(int x) {
int cap = static_cast<int>(data.size());
if (count == cap) return false; // full
data[(head + count) % cap] = x; // the slot after the rear, wrapping to the start
count++;
return true;
}
int dequeue() {
if (count == 0) return -1; // empty
int x = data[head];
head = (head + 1) % static_cast<int>(data.size()); // the front moves one slot, wrapping
count--;
return x;
}
int front() {
return count == 0 ? -1 : data[head];
}
int rear() {
return count == 0 ? -1 : data[(head + count - 1) % static_cast<int>(data.size())];
}
bool isEmpty() {
return count == 0;
}
bool isFull() {
return count == static_cast<int>(data.size());
}
};Python
class FixedQueue:
def __init__(self, capacity: int):
self.data = [0] * capacity
self.head = 0 # the front is data[head]
self.count = 0 # the rear is count - 1 places later, wrapping
def enqueue(self, x: int) -> bool:
if self.count == len(self.data):
return False # full
self.data[(self.head + self.count) % len(self.data)] = x # the slot after the rear, wrapping
self.count += 1
return True
def dequeue(self) -> int:
if self.count == 0:
return -1 # empty
x = self.data[self.head]
self.head = (self.head + 1) % len(self.data) # the front moves one slot, wrapping
self.count -= 1
return x
def front(self) -> int:
return -1 if self.count == 0 else self.data[self.head]
def rear(self) -> int:
return -1 if self.count == 0 else self.data[(self.head + self.count - 1) % len(self.data)]
def isEmpty(self) -> bool:
return self.count == 0
def isFull(self) -> bool:
return self.count == len(self.data)Common mistakes
- Using
rear == frontto mean both full and empty. - Forgetting the modulo when computing the rear.