A Double-Ended Queue (Deque) is a generalized version of a queue that allows insert and delete operations from both the Front and the Rear. It can act as both a Stack and a Queue simultaneously!
from collections import deque
# Python's deque is natively implemented
# as a double-ended queue.
q = deque()
q.append(1) # Insert Rear
q.appendleft(2) # Insert Front
q.popleft() # Delete Front
q.pop() # Delete Rear
class Deque {
constructor() {
this.items = [];
}
insertFront(elem) { this.items.unshift(elem); }
insertRear(elem) { this.items.push(elem); }
deleteFront() { return this.items.shift(); }
deleteRear() { return this.items.pop(); }
}