How to Implement stack using a priority queue(using min heap)?.

**Asked In: Microsoft, Adobe.**

**Solution:**

In priority queue, we assign priority to the elements that are being pushed. A stack requires elements to be processed in Last in First Out manner. The idea is to associate a count that determines when it was pushed. This count works as a key for the priority queue.

So the implementation of stack uses a priority queue of pairs, with the first element serving as the key.

`pair <` `int` `, ` `int` `> (key, value)` |

*chevron_right*

*filter_none*

**See Below Image to understand Better **

Below is C++ implementation of the idea.

`// C++ program to implement a stack using ` `// Priority queue(min heap) ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `typedef` `pair<` `int` `, ` `int` `> pi; ` ` ` `// User defined stack class ` `class` `Stack{ ` ` ` ` ` `// cnt is used to keep track of the number of ` ` ` `//elements in the stack and also serves as key ` ` ` `//for the priority queue. ` ` ` `int` `cnt; ` ` ` `priority_queue<pair<` `int` `, ` `int` `> > pq; ` `public` `: ` ` ` `Stack():cnt(0){} ` ` ` `void` `push(` `int` `n); ` ` ` `void` `pop(); ` ` ` `int` `top(); ` ` ` `bool` `isEmpty(); ` `}; ` ` ` `// push function increases cnt by 1 and ` `// inserts this cnt with the original value. ` `void` `Stack::push(` `int` `n){ ` ` ` `cnt++; ` ` ` `pq.push(pi(cnt, n)); ` `} ` ` ` `// pops element and reduces count. ` `void` `Stack::pop(){ ` ` ` `if` `(pq.empty()){ cout<<` `"Nothing to pop!!!"` `;} ` ` ` `cnt--; ` ` ` `pq.pop(); ` `} ` ` ` `// returns the top element in the stack using ` `// cnt as key to determine top(highest priority), ` `// default comparator for pairs works fine in this case ` `int` `Stack::top(){ ` ` ` `pi temp=pq.top(); ` ` ` `return` `temp.second; ` `} ` ` ` `// return true if stack is empty ` `bool` `Stack::isEmpty(){ ` ` ` `return` `pq.empty(); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `Stack* s=` `new` `Stack(); ` ` ` `s->push(1); ` ` ` `s->push(2); ` ` ` `s->push(3); ` ` ` `while` `(!s->isEmpty()){ ` ` ` `cout<<s->top()<<endl; ` ` ` `s->pop(); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

Output:

3 2 1

Now, as we can see this implementation takes O(log n) time for both push and pop operations. This can be slightly optimized by using fibonacci heap implementation of priority queue which would give us O(1) time complexity for push operation, but pop still requires O(log n) time.

This article is contributed by **Mr. Somesh Awasthi**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Priority Queue using Binary Heap
- Why is Binary Heap Preferred over BST for Priority Queue?
- Priority Queue using Queue and Heapdict module in Python
- Difference between Circular Queue and Priority Queue
- Implement a stack using single queue
- Implement Stack and Queue using Deque
- Stack and Queue in Python using queue Module
- Check if a queue can be sorted into another queue using a stack
- Priority Queue using Linked List
- Priority Queue using doubly linked list
- Huffman Coding using Priority Queue
- Find the K closest points to origin using Priority Queue
- Priority Queue in C++ Standard Template Library (STL)
- Priority Queue | Set 1 (Introduction)
- Applications of Priority Queue
- Implementation of Priority Queue in Javascript
- Priority Queue in Python
- Double ended priority queue
- Implement Stack using Queues
- Implement a stack using singly linked list