Deque (Double-ended Queue)
Deque Operations in C++
C++ Deque (Double-ended Queue) Program
This program helps you to learn the fundamental structure and syntax of C++ programming.
#include <iostream>
#include <deque>
using namespace std;
int main() {
// Create deque
deque<int> dq;
// Add elements at both ends
dq.push_back(10);
dq.push_back(20);
dq.push_back(30);
dq.push_front(5);
dq.push_front(1);
cout << "Deque elements: ";
for (int num : dq) {
cout << num << " ";
}
cout << endl;
cout << "Front: " << dq.front() << endl;
cout << "Back: " << dq.back() << endl;
cout << "Size: " << dq.size() << endl;
// Access elements by index
cout << "Element at index 2: " << dq[2] << endl;
cout << "Element at index 2 (at): " << dq.at(2) << endl;
// Remove from front
dq.pop_front();
cout << "\nAfter pop_front: ";
for (int num : dq) {
cout << num << " ";
}
cout << endl;
// Remove from back
dq.pop_back();
cout << "After pop_back: ";
for (int num : dq) {
cout << num << " ";
}
cout << endl;
// Insert at specific position
dq.insert(dq.begin() + 1, 15);
cout << "\nAfter inserting 15 at index 1: ";
for (int num : dq) {
cout << num << " ";
}
cout << endl;
// Erase element
dq.erase(dq.begin() + 1);
cout << "After erasing element at index 1: ";
for (int num : dq) {
cout << num << " ";
}
cout << endl;
return 0;
}Deque elements: 1 5 10 20 30 Front: 1 Back: 30 Size: 5 Element at index 2: 10 Element at index 2 (at): 10 After pop_front: 5 10 20 30 After pop_back: 5 10 20 After inserting 15 at index 1: 5 15 10 20 After erasing element at index 1: 5 10 20
Understanding Deque (Double-ended Queue)
This program teaches you how to use Deque (Double-ended Queue) in C++. Deque allows efficient insertion and deletion at both ends, combining the benefits of vectors and queues. It provides random access like vectors but with efficient front operations.
---
1. What This Program Does
The program demonstrates deque operations:
Deques provide flexible, efficient double-ended operations.
---
2. Header Files Used
---
3. Understanding Deque
Double-ended Queue Concept
:
Key Features
:
---
4. Adding Elements
At Back
:
dq.push_back(10); // Add to end
At Front
:
dq.push_front(5); // Add to beginning
How it works
:
---
5. Removing Elements
From Back
:
dq.pop_back(); // Remove from end
From Front
:
dq.pop_front(); // Remove from beginning
How it works
:
---
6. Accessing Elements
Random Access
:
dq[2] // Element at index 2
dq.at(2) // Element at index 2 (with bounds check)
Front and Back
:
dq.front() // First element
dq.back() // Last element
How it works
:
---
7. Inserting at Position
Using insert()
:
dq.insert(dq.begin() + 1, 15);
How it works
:
---
8. When to Use Deque
Best For
:
Example Scenarios
:
---
9. Important Considerations
Performance
:
Memory Layout
:
Flexibility
:
---
10. return 0;
This ends the program successfully.
---
Summary
This program is fundamental for learning double-ended containers, understanding flexible data structures, and preparing for algorithms requiring both ends manipulation in C++ programs.
Let us now understand every line and the components of the above program.
Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.
Practical Learning Notes for Deque (Double-ended Queue)
This C++ program is part of the "STL Containers Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.
A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.
For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.