![]() |
VOOZH | about |
A deque (Double-Ended Queue) is a sequence container in the C++ Standard Template Library (STL) that combines the advantages of both vectors and queues. It supports efficient operations at both ends while allowing direct access to elements.
The above illustration shows that elements can be inserted and removed efficiently from both the front and back of a deque.
10 20 30 40
Explanation
Syntax
The std::deque class template is defined inside the <deque> header file.
deque<T>d;
where,
The following are the most commonly used operations on a deque.
A deque supports insertion at both ends using:
1. push_back()
The push_back() function inserts a new element at the end of the deque.
Elements in deque (added using push_back): 10 20 30
Explanation: The elements 10, 20, and 30 are inserted at the back of the deque in the same order.
2. push_front()
The push_front() function inserts a new element at the front of the deque.
Elements in deque (added using push_front): 10 20 30
Explanation: Each element is inserted at the front, so the final order becomes 10 20 30.
A deque supports deletion from both ends using:
1. pop_back()
The pop_back() function removes the last element from the deque.
Original deque: 10 20 30 40 Deque after pop_back(): 10 20 30
2. pop_front()
The pop_front() function removes the first element from the deque.
Original deque: 10 20 30 40 Deque after pop_front(): 20 30 40
Explanation: The first element (10) is removed, leaving 20 30 40.
Deque provides functions to access elements from both the front and the back without removing them.
1. front()
The front() function returns a reference to the first element of the deque.
The first element (front) is: 100
Explanation: The front() function returns the first element of the deque, which is 100.
2. back()
The back() function returns a reference to the last element of the deque.
The last element (back) is: 40
Explanation: The back() function returns the last element of the deque, which is 40.
The following functions are commonly used to check the size and state of a deque.
1. size()
The size() function returns the number of elements currently stored in the deque.
The number of elements in the deque is: 4
Explanation: The size() function returns the total number of elements present in the deque.
2. empty()
The empty() function checks whether the deque contains any elements.
Deque is empty. Deque is not empty.
Explanation: Initially the deque contains no elements, so empty() returns true. After inserting an element, it returns false.
3. Clear()
The clear() function removes all elements from the deque.
Before clear(), size: 5 After clear(), size: 0 Deque is now empty.
Explanation: The clear() function removes all elements from the deque, leaving it empty.
The table below summarizes the time complexity of common deque operations.
| Operation | Time Complexity |
|---|---|
| Insert at back | O(1) amortized |
| Insert at front | O(1) amortized |
Insert at arbitrary position | O(n) |
| Remove from back | O(1) amortized |
| Remove from front | O(1) amortized |
Remove from arbitrary position | O(n) |
| Access elements at any position using index | O(1) |
| Update elements at any position using index | O(1) |
| Iterate the deque | O(n) |
The following table highlights the key differences between a queue and a deque based on their structure, operations, and common use cases.
Feature | Queue | Deque(Double-Ended Queue) |
|---|---|---|
Definition | A linear data structure that follows FIFO (First-in-First-Out). | A generalized version of queue that allows insertion and deletion from both ends. |
Operations Allowed | Enqueue (add to rear) and Dequeue (remove from front) | Insert Front, Insert Rear, Delete Front, Delete Rear |
Access | Restricted: insertion at rear and deletion at front only. | More flexible: insertions and deletions at both front and rear. |
Use Case | When you need strict FIFO ordering (e.g., task scheduling) | When you need both FIFO and LIFO behavior (e.g., sliding window problems, palindrome checking). |
Efficiency | Simpler, but limited in functionality | Slightly more complex, but more powerful |
Types | Simple Queue, Circular Queue, Priority Queue | Input-Restricted Deque (insert rear only) and Output-Restricted Deque (delete front only) |