How to Create a Stack of Queue in C++? Last Updated : 13 Mar, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In C++, the std::stack is a container that follows the LIFO (Last In, First Out) rule, whereas std::queue is a container that follows the FIFO (First In, First Out) rule. In this article, we will learn how to create a stack of a queue in C++. Example: Input:myQueue = { a, b, c };myQueue = { d, e };Output:stackOfQueue = [ { a, b, c }, { d, e } ]Stack of Queues in C++We can create a stack of queues in C++ by passing the std::queue type as the template argument during the declaration of the stack. In this way, each element of the stack will be a queue in itself. Syntax to Declare Stack of Queues in C++stack< queue<datatype>> stack_name;Here, datatype denotes the type of data you want to store in the queue.stack_name is the name of the stack of queues.C++ Program to Create Stack of QueueThe below program demonstrates how we can create a stack of queue in C++ STL. C++ // C++ Program to illustrate how to create a stack of queues #include <iostream> #include <queue> #include <stack> using namespace std; int main() { // Define the type of queue typedef queue<int> QueueType; // Initialize two queues QueueType queue1, queue2; queue1.push(1); queue1.push(2); queue1.push(3); queue2.push(4); queue2.push(5); // Create a stack of queues stack<QueueType> myStack; myStack.push(queue1); myStack.push(queue2); // Print the stack of queues while (!myStack.empty()) { QueueType& topQueue = myStack.top(); while (!topQueue.empty()) { cout << topQueue.front() << ", "; topQueue.pop(); } cout << endl; myStack.pop(); } return 0; } Output4, 5, 1, 2, 3, Time Complexity: O(N), where N is the number of queues.Auxiliary Space: O(N * M), where M is the size of each queue. Comment More infoAdvertise with us Next Article How to Create a Stack of Queue in C++? P pantharshx9d9 Follow Improve Article Tags : C++ Programs C++ STL cpp-queue cpp-stack CPP Examples +2 More Practice Tags : CPPSTL Similar Reads How to Create a Stack of Priority_Queue in C++? In C++, std::stack is a container that follows the LIFO (Last In, First Out) rule, whereas std::priority_queue is a type of queue in which the first element is either the greatest(by default) or the smallest of all elements in the queue. In this article, we will learn how to create a stack of a prio 2 min read How to Create a Stack of Stack in C++? In C++, the stack is a container that follows the LIFO (Last In, First Out) order in which the elements are inserted and removed from it. In this article, we will learn how to create a stack of a stack in C++. Example:Input:Elements in stack1= 1, 2, 3, 4Elements in stack2= 5, 6, 7Output:Elements in 2 min read How to Create a Stack of Deque in C++? 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 2 min read How to Create a Stack of Lists in C++? In C++, a list is a sequence container that allows dynamic insertion and deletion operations, whereas a stack is a data structure that follows last-in, first-out (LIFO). In this article, we will learn how to create a stack of lists in C++. Example: Input: list1 = { 1, 2, 3, 4 }list2 = { 5, 6, 7 }Out 2 min read How to Create a Stack of Arrays in C++? In C++, the std::stack is a container that follows the LIFO (Last In, First Out) rule, whereas std::array is a sequence container that stores elements in contiguous memory. In this article, we will learn how to create a stack of an array in C++. Example: Input: arr1 = {1, 2, 3}; arr2 = {4, 5, 6}; ar 2 min read Like