Ready — enter an integer to insert
Speed
🎨 Customize Colors
Scene Background
Element Color
Value Color
Pointer Color

🚥 Deque (Double Ended Queue)

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!

⏱ Access: O(n) ⏱ Insert Front/Rear: O(1) ⏱ Delete Front/Rear: O(1) 💾 Space: O(n)
Python
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
JavaScript
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(); }
}