Open In App

How Deque Works Internally in C++?

Last Updated : 21 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisite: Deque in C++

Deque or Double Ended Queue is a generalized version of the Queue data structure that allows insert and deletion at both ends. It supports the access of elements in O(1) time complexity and the insertion and deletion of elements from front and back are both done with O(1) time complexity.

Practical Applications of Deque

Deque’s ability to insert and delete from both sides makes it one of the most useful containers in STL. Real-world applications are mentioned below:

  1. Applied as both stack and queue, as it supports both operations.
  2. Storing a web browser’s history.
  3. Storing a software application’s list of undo operations.
  4. Job scheduling algorithm

How Deque Works Internally in C++ STL

An STL deque is implemented using arrays of data or arrays of pointers to memory blocks rather than a linked list. Depending on storage requirements, the number of blocks and size of the array of pointers fluctuate dynamically. These memory blocks contain items at adjacent locations.

A block of memory is automatically allocated when a deque object is created so that the objects can be stored in contiguous locations.

  1. Deque then allocates a new block of memory and joins the front of the prior memory block with it when we put an item in front of it. Now, if we add pieces to the front once more, they will be stored in this new memory block until it is entirely full.
  2. When an item is inserted at the end of a deque, the allocated block of memory holds it until it is completely filled; if this occurs, a new block of memory is allocated and connected to the end of the preceding block. Elements that are added to the deque’s back are now stored in that new memory block.

Since a deque does not need shifting an element by 1 as an array does in a block of memory to make room at the front, you can think of it as a linked list of vectors. In order to allocate a new block of memory to it, it first determines if there is space remaining at the front of the first element in the current block of memory.

Time Complexity:

  • Accessing Elements- O(1)
  • Insertion or removal of elements- O(N)
  • Insertion or removal of elements at start or end- O(1)

Example:

C++




// C++ code to show working of the deque
#include <deque>
#include <iostream>
using namespace std;
  
// Driver Code
int main()
{
    deque<int> d = { 1, 2, 3 };
  
    d.push_back(4);
    d.push_front(0);
  
    cout << "Elements in Deque: " << endl;
  
    for (int i : d)
        cout << i << " ";
    cout << endl;
  
    d.pop_back();
  
    cout << "\n"
         << "Elements in Deque after pop_back(): " << endl;
  
    for (int i : d)
        cout << i << " ";
    cout << endl;
  
    cout << "\n"
         << "Elements in Deque after pop_front(): " << endl;
  
    d.pop_front();
  
    for (int i : d)
        cout << i << " ";
    cout << endl;
  
    cout << "\n"
         << "Element in front of deque: " << d.front()
         << endl;
    cout << "Element in back of deque: " << d.back()
         << endl;
  
    cout << "Item at 1th Index: " << d.at(1) << endl;
  
    cout << "Size of deque: " << d.size() << endl;
    cout << "Is deque empty: " << d.empty() << endl;
  
    cout << "\n"
         << "After deleting all elements of deque:"
         << "\n\n";
  
    d.erase(d.begin(), d.end());
  
    cout << "Size of deque: " << d.size() << endl;
    cout << "Is deque empty: " << d.empty() << endl;
}


Output

Elements in Deque: 
0 1 2 3 4 

Elements in Deque after pop_back(): 
0 1 2 3 

Elements in Deque after pop_front(): 
1 2 3 

Element in front of deque: 1
Element in back of deque: 3
Item at 1th Index: 2
Size of deque: 3
Is deque empty: 0

After deleting all elements of deque:

Size of deque: 0
Is deque empty: 1


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads