Deque (Double-ended Queue) in C++

Deque Operations in C++.

IntermediateSTL Containers ProgramsExample 10 of 10
deque-double-ended-queue.cpp
1#include <iostream>
2#include <deque>
3using namespace std;
4
5int main() {
6 // Create deque
7 deque<int> dq;
8
9 // Add elements at both ends
10 dq.push_back(10);
11 dq.push_back(20);
12 dq.push_back(30);
13 dq.push_front(5);
14 dq.push_front(1);
15
16 cout << "Deque elements: ";
17 for (int num : dq) {
18 cout << num << " ";
19 }
20 cout << endl;
21
22 cout << "Front: " << dq.front() << endl;
23 cout << "Back: " << dq.back() << endl;
24 cout << "Size: " << dq.size() << endl;
25
26 // Access elements by index
27 cout << "Element at index 2: " << dq[2] << endl;
28 cout << "Element at index 2 (at): " << dq.at(2) << endl;
29
30 // Remove from front
31 dq.pop_front();
32 cout << "\nAfter pop_front: ";
33 for (int num : dq) {
34 cout << num << " ";
35 }
36 cout << endl;
37
38 // Remove from back
39 dq.pop_back();
40 cout << "After pop_back: ";
41 for (int num : dq) {
42 cout << num << " ";
43 }
44 cout << endl;
45
46 // Insert at specific position
47 dq.insert(dq.begin() + 1, 15);
48 cout << "\nAfter inserting 15 at index 1: ";
49 for (int num : dq) {
50 cout << num << " ";
51 }
52 cout << endl;
53
54 // Erase element
55 dq.erase(dq.begin() + 1);
56 cout << "After erasing element at index 1: ";
57 for (int num : dq) {
58 cout << num << " ";
59 }
60 cout << endl;
61
62 return 0;
63}

Output

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

What's going on

The program demonstrates deque operations:

Adding elements at front and back
Removing elements from front and back
Random access using index
Inserting and erasing at specific positions

Deques provide flexible, efficient double-ended operations.

Header Files Used

1.#include <iostream>
Provides cout and cin for input/output operations.
2.#include <deque>
Provides deque container class.

Understanding Deque

Double-ended Queue Concept

:

Insert/delete at both ends
Random access like vectors
Efficient front and back operations
Collection of memory blocks

Key Features

:

O(1) operations at both ends
Random access (like arrays)
Flexible insertion/deletion
Combines vector and queue benefits

Adding Elements

At Back

:

dq.push_back(10); // Add to end

At Front

:

dq.push_front(5); // Add to beginning

How it works

:

push_back(): adds to end (like vector)
push_front(): adds to beginning (unique to deque)
Both O(1) operations
Flexible insertion

Removing Elements

From Back

:

dq.pop_back(); // Remove from end

From Front

:

dq.pop_front(); // Remove from beginning

How it works

:

pop_back(): removes from end
pop_front(): removes from beginning
Both O(1) operations
Flexible deletion

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

:

[] operator: array-like access
at(): bounds-checked access
front()/back(): end elements
O(1) access

Inserting at Position

Using insert()

:

dq.insert(dq.begin() + 1, 15);

How it works

:

Inserts at specified position
Uses iterator arithmetic
O(n) operation (may shift elements)
Flexible but expensive for large deques

When to Use Deque

Best For

:

Need both stack and queue operations
Random access required
Efficient front and back operations
Sliding window problems
Both ends manipulation

Example Scenarios

:

Sliding window maximum
Palindrome checking
Both ends processing
Queue with random access
Double-ended operations

Important Considerations

Performance

:

Front/back operations: O(1)
Random access: O(1)
Middle insert/erase: O(n)
More efficient than vector for front operations

Memory Layout

:

Collection of memory blocks
Not contiguous like vector
Slightly less cache-friendly
More flexible memory management

Flexibility

:

Combines vector and queue
Best of both worlds
Use when need both capabilities
More versatile than vector or queue alone

return 0;

This ends the program successfully.