Deque : A linear data structure that allows insertion and deletion from both ends (front and rear). It is also called a Double Ended Queue.
Unlike Queue (FIFO), Deque provides flexibility to insert and remove elements from either side.
👁 Deque
Operations of Deque:
insertFront(x)
- Insert element at the front
- Array (Circular): O(1)
- Doubly Linked List: O(1)
- Space: O(1)
insertRear(x)
- Insert element at the rear
- Array (Circular): O(1)
- Doubly Linked List: O(1)
- Space: O(1)
deleteFront()
- Remove element from the front
- Array (Circular): O(1)
- Doubly Linked List: O(1)
- Space: O(1)
deleteRear()
- Remove element from the rear
- Array (Circular): O(1)
- Doubly Linked List: O(1)
- Space: O(1)
getFront()
- View front element without removing
- Array / Linked List: O(1)
- Space: O(1)
getRear()
- View rear element without removing
- Array / Linked List: O(1)
- Space: O(1)
isEmpty()
- Check if deque is empty
- Array / Linked List: O(1)
isFull()
- Check if deque is full (only for fixed-size array)
- Array: O(1)
- Linked List: Not required (dynamic size)
Implementation of Deque:
Deque Implementation using Array (Circular Array):
Using an array with two pointers:
Pointers:
- front: Points to first element
- rear: Points to last element
Operations:
- insertFront(x): O(1)
- insertRear(x): O(1)
- deleteFront(): O(1)
- deleteRear(): O(1)
- getFront() / getRear(): O(1)
Advantages:
- Efficient O(1) operations
- No shifting required
- Better than normal array queue
Limitation:
- Fixed size
- Overflow possible if capacity is reached
OutputFront: 5
Rear: 20
Front: 10
Deque Implementation using Doubly Linked List:
Each node contains:
Pointers:
- frontPtr: Points to first node
- rearPtr: Points to last node
Operations:
- insertFront: O(1)
- insertRear: O(1)
- deleteFront: O(1)
- deleteRear: O(1)
- getFront / getRear: O(1)
Advantages:
- Dynamic size
- No overflow
- Efficient insertion and deletion
Limitation:
- Extra memory for prev pointer
OutputFront: 5
Rear: 20
Front: 10
👁 advantages_of_deque
Also Check: