Deque stands for Double-Ended Queue. It's a sequence container that allows you to add or remove elements efficiently from both the front and the back.
- Deque can act as both stack and Queue.
- It is useful in many problems where we need to have a subset of all operations also like insert/remove at front and insert/remove at the end.
C++
#include <deque>
#include <iostream>
using namespace std;
int main()
{
// Declare an empty deque of integers
deque<int> d1;
// Declare and initialize a deque with some values
deque<int> d2 = {10, 20, 30, 40};
for (int val : d2) {
cout << val << " ";
}
cout << endl;
return 0;
}
Syntax
The std::deque class template is defined inside the <deque> header file.
deque<T>d;
where,
- T: Data type of elements in the deque
- d: Name assigned to the deque
Basic Operations
The basic operations on deque are shown below:
Inserting Elements
In deque, there are 2 operations of insertion push_back() and push_front().
push_back():
Adds an element at the end of the deque.
Useful for appending values.
C++
#include <iostream>
#include <deque>
using namespace std;
int main() {
deque<int> d;
// Adding elements at the back
d.push_back(10);
d.push_back(20);
d.push_back(30);
// Displaying elements
cout << "Elements in deque (added using push_back): ";
for (int val : d) {
cout << val << " ";
}
cout << endl;
return 0;
}
OutputElements in deque (added using push_back): 10 20 30
push_front()
Adds an element at the front of the deque.
Helps insert values at the beginning efficiently.
C++
#include <iostream>
#include <deque>
using namespace std;
int main() {
deque<int> d;
// Adding elements at the front
d.push_front(30);
d.push_front(20);
d.push_front(10);
// Displaying elements
cout << "Elements in deque (added using push_front): ";
for (int val : d) {
cout << val << " ";
}
cout << endl;
return 0;
}
OutputElements in deque (added using push_front): 10 20 30
Deletion
In deque there are 2 operations of deletion pop_back() and pop_front().
pop_back()
Removes the last element from the deque.
Used when you want to delete from the back.
C++
#include <iostream>
#include <deque>
using namespace std;
int main() {
deque<int> d = {10, 20, 30, 40};
cout << "Original deque: ";
for (int val : d) {
cout << val << " ";
}
cout << endl;
// Removing the last element
d.pop_back();
cout << "Deque after pop_back(): ";
for (int val : d) {
cout << val << " ";
}
cout << endl;
return 0;
}
OutputOriginal deque: 10 20 30 40
Deque after pop_back(): 10 20 30
pop_front()
Removes the first element from deque.
Efficient way to remove from the front.
C++
#include <iostream>
#include <deque>
using namespace std;
int main() {
deque<int> d = {10, 20, 30, 40};
cout << "Original deque: ";
for (int val : d) {
cout << val << " ";
}
cout << endl;
// Removing the first element
d.pop_front();
cout << "Deque after pop_front(): ";
for (int val : d) {
cout << val << " ";
}
cout << endl;
return 0;
}
OutputOriginal deque: 10 20 30 40
Deque after pop_front(): 20 30 40
Accessing Elements
Elements in a deque can be accessed using front() and back().
front()
Returns the first element.
Does not remove it.
C++
#include <iostream>
#include <deque>
using namespace std;
int main() {
deque<int> d = {100, 200, 300};
// Accessing the front element
cout << "The first element (front) is: "
<< d.front() << endl;
return 0;
}
OutputThe first element (front) is: 100
back()
Returns the last element.
Useful to check the end value.
C++
#include <iostream>
#include <deque>
using namespace std;
int main() {
deque<int> d = {10, 20, 30, 40};
// Accessing the last element
cout << "The last element (back) is: "
<< d.back() << endl;
return 0;
}
OutputThe last element (back) is: 40
Size or empty
size()
Returns the number of elements.
Helps in loops or checking length.
C++
#include <iostream>
#include <deque>
using namespace std;
int main() {
deque<int> d = {5, 10, 15, 20};
// Get the size of the deque
cout << "The number of elements in the deque is: "
<< d.size() << endl;
return 0;
}
OutputThe number of elements in the deque is: 4
empty()
Returns true if deque is empty.
Useful for errors before accessing.
C++
#include <iostream>
#include <deque>
using namespace std;
int main() {
deque<int> d;
if (d.empty()) {
cout << "Deque is empty." << endl;
} else {
cout << "Deque is not empty." << endl;
}
// Add an element and check again
d.push_back(100);
if (d.empty()) {
cout << "Deque is empty." << endl;
} else {
cout << "Deque is not empty." << endl;
}
return 0;
}
OutputDeque is empty.
Deque is not empty.
Clear
Removes all elements from deque.
Deque becomes empty after this.
C++
#include <iostream>
#include <deque>
using namespace std;
int main() {
deque<int> d = {1, 2, 3, 4, 5};
cout << "Before clear(), size: " << d.size() << endl;
// Clear all elements from deque
d.clear();
cout << "After clear(), size: " << d.size() << endl;
if (d.empty()) {
cout << "Deque is now empty." << endl;
}
return 0;
}
OutputBefore clear(), size: 5
After clear(), size: 0
Deque is now empty.
Time Complexity
The below table lists the time complexity of the above operations on deque:
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) |
Queue vs Deque
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) 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) Output-Restricted Deque (delete front only) |
---|
Explore
C++ Basics
Core Concepts
OOP in C++
Standard Template Library(STL)
Practice & Problems