Open In App

How to Create a Stack of Deque in C++?

Last Updated : 11 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In C++, the stack is a container in which new elements are added from one end (top) and removed from that end only whereas a deque (double-ended queue) are sequence container with the feature of expansion and contraction on both ends. In this article, we will learn how to create a stack of deque in C++.

Example:

Input: 
myDeque1 = 1, 2, 3, 4
myDeque2 = 5, 6, 7

Output:
Stack of Deque: [ {5, 6, 7},
{1, 2, 3, 4} ]

Stack of Deque in C++

To create a stack of deques in C++, we need to pass the std::deque as the template parameter in the declaration of the stack. We can then use the std::stack::push() function to insert the deque container in the stack.

Syntax to Create a Stack of Deque in C++

stack<deque<datatype>> stack_name; 

Here,

  • datatype denotes the type of data stored in the deque.
  • stack_name is the name of the stack of deque.

C++ Program to Use a Stack with a Deque

The below program demonstrates how we can create a stack of deque in C++.

C++




// C++ Program to illustrate how to crate a stack of deque
#include <deque>
#include <iostream>
#include <stack>
using namespace std;
  
int main()
{
    // Creating a stack of deques
    stack<deque<int> > stackOfDeques;
  
    // Pushing deques into the main stack (stackOfDeques)
    deque<int> d1, d2;
    d1.push_back(1);
    d1.push_back(2);
    d1.push_back(3);
    d1.push_back(4);
    d2.push_back(5);
    d2.push_back(6);
    d2.push_back(7);
  
    // push the deque in stack of deques
    stackOfDeques.push(d1);
    stackOfDeques.push(d2);
  
    // Printing elements from the stack of deques
    cout << "Elements in the Stack of Deque:" << endl;
    while (!stackOfDeques.empty()) {
        deque<int> currD = stackOfDeques.top();
        stackOfDeques.pop();
  
        while (!currD.empty()) {
            cout << currD.back() << " ";
            currD.pop_back();
        }
    }
  
    return 0;
}


Output

Elements in the Stack of Deque:
7 6 5 4 3 2 1 

Time Complexity: O(N), here N is the total number of deque.
Auxiliary Space: O(N * M), where M is the average number of elements in the deque.



Similar Reads

deque::at() and deque::swap() in C++ STL
Deque or Double ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. deque::at()at() function is us
4 min read
deque::pop_front() and deque::pop_back() in C++ STL
Deque or Double ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. deque::pop_front() pop_front()
4 min read
deque::clear() and deque::erase() in C++ STL
Deque or Double-ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in the case of insertion and deletion of elements at the end, and also at the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. deque::clear() The clear()
5 min read
deque::operator= and deque::operator[] in C++ STL
Deque or Double ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. deque::operator= This operator
4 min read
deque::begin() and deque::end in C++ STL
Deque or Double ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. deque::begin()begin() function
3 min read
deque::emplace_front() and deque::emplace_back() in C++ STL
Deque or Double-ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. deque::emplace_front()This fun
5 min read
Difference Between deque::cbegin and deque::assign in C++
Deque or Double-ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in the case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. Here we will see the differenc
3 min read
Difference Between deque::assign and deque::at in C++
Deque or Double-ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in the case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. Here we will see the differenc
3 min read
Difference Between deque::assign and deque::back in C++
Deque or Double-ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in the case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. Deque::assign deque::assign is
3 min read
Difference Between deque::begin and deque::assign in C++
Deque or Double-ended queue are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in the case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. Here we will see the difference
3 min read
Practice Tags :