How to Create a Stack of Priority_Queue in C++?
Last Updated :
13 Mar, 2024
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 priority_queue in C++.
Example:
Input:
pq1 = { 1, 2, 3, 4, 5 };
pq2 = {1, 5, 9};
Output:
pqStack = [ {1, 5, 9},
{ 1, 2, 3, 4, 5 } ]
Stack of Priority Queues in C++
To create a stack of priority_queues in C++, we have to pass the std::priority_queue type as the template argument during the declaration of the stack.
Syntax to Declare Stack of Priority_Queues in C++
stack< priority_queue<datatype>> stack_name;
Here,
datatype
denotes the type of data we want to store in the priority queue.stack_name
is the name of the stack of priority queue.
C++ Program to Create Stack of Priority Queue
The below program demonstrates how we can create a stack of priority_queue in C++ STL.
C++
// C++ Program to illustrate how to create a stack of
// priority_queues
#include <iostream>
#include <queue>
#include <stack>
using namespace std;
int main()
{
// Define the type of priority_queue
typedef priority_queue<int> PriorityQueueType;
// Initialize two priority_queues
PriorityQueueType pq1, pq2;
pq1.push(1);
pq1.push(2);
pq1.push(3);
pq2.push(4);
pq2.push(5);
// Create a stack of priority_queues
stack<PriorityQueueType> myStack;
myStack.push(pq1);
myStack.push(pq2);
// Print the stack of priority_queues
while (!myStack.empty()) {
PriorityQueueType& topQueue = myStack.top();
while (!topQueue.empty()) {
cout << topQueue.top() << ", ";
topQueue.pop();
}
cout << endl;
myStack.pop();
}
return 0;
}
Time Complexity: O(N), here N is the number of priority_queues.
Auxiliary Space: O(N * M), where M is the size of each priority_queue.
Similar Reads
How to Create a Stack of Queue in C++? 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 };O
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 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 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 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