Sub topic 11
Deques and Restricted Queues
Assume you know the queue ADT, arrays, circular indexing, and Big-O notation. A deque is a double-ended queue. It allows insertion and deletion at both the front and the rear. The name comes from “double-ended queue.”
A deque supports these operations:
insert_front(x): addxat the front.insert_rear(x): addxat the rear.delete_front(): remove and return the front item.delete_rear(): remove and return the rear item.get_front()andget_rear(): inspect an endpoint without removing it.
With a circular array and two endpoint positions, each operation takes O(1) time. The array capacity is fixed unless the implementation resizes it.
Circular-array implementation
The front index points to the first item. The size field tells us how many positions are occupied. The rear item is at index (front + size - 1) % capacity.
class Deque:
def __init__(self, capacity):
if capacity <= 0:
raise ValueError("capacity must be positive")
self.items = [None] * capacity
self.front = 0
self.size = 0
def insert_front(self, value):
if self.size == len(self.items):
raise OverflowError("deque full")
self.front = (self.front - 1) % len(self.items)
self.items[self.front] = value
self.size += 1
def insert_rear(self, value):
if self.size == len(self.items):
raise OverflowError("deque full")
index = (self.front + self.size) % len(self.items)
self.items[index] = value
self.size += 1
def delete_front(self):
if self.size == 0:
raise IndexError("deque empty")
value = self.items[self.front]
self.items[self.front] = None
self.front = (self.front + 1) % len(self.items)
self.size -= 1
return value
def delete_rear(self):
if self.size == 0:
raise IndexError("deque empty")
index = (self.front + self.size - 1) % len(self.items)
value = self.items[index]
self.items[index] = None
self.size -= 1
return value
d = Deque(4)
d.insert_rear(10)
d.insert_front(5)
d.insert_rear(20)
print(d.delete_front()) # 5
print(d.delete_rear()) # 20When the front moves below index 0, modulo arithmetic wraps it to the last array position. When the deque is empty, size == 0. When it is full, size == capacity. Tracking size avoids confusing these two states.
Restricted deques
A restricted deque removes one kind of endpoint operation:
| Structure | Allowed insertion | Allowed deletion |
|---|---|---|
| General deque | Front or rear | Front or rear |
| Input-restricted deque | One fixed end | Front or rear |
| Output-restricted deque | Front or rear | One fixed end |
An input-restricted deque is useful when new data must enter through one controlled side, but either end may be removed. An output-restricted deque is useful when data may arrive at either side, but removal must follow one controlled side.
GATE-style reasoning
For an array-based deque, endpoint operations are O(1) when the array has free space. A linked-list deque also supports O(1) operations if it stores both front and rear references. A singly linked list cannot delete the rear in O(1) time because it must find the node before the rear.
Exercise
In an input-restricted deque, which operations are allowed?
Show answer
Insertion is allowed at only one fixed end, while deletion is allowed at either end.