-
Notifications
You must be signed in to change notification settings - Fork 369
/
Copy pathStack_using_Priority_queue.cpp
95 lines (88 loc) · 2.59 KB
/
Stack_using_Priority_queue.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// ====================== Problem Statement ==========================
/*
Here we will take the numbers input from user and with the help of priority queue we need to implement stack.
Priority queue is a special type of queue in which the numbers are dequeue based on their priority. Every no has some priority along with its data.
The data having the highest priority is popped first.
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.
For ex:
User enters input : 4 1 2
so in queue -> [(2,3) (1,2) (4,1)]
*/
// // ====================== Code with C++ ==========================
#include<iostream>
#include<climits>
#include<queue>
using namespace std;
typedef pair<int, int> pr;
class Queue{
int count;//storing the count of no of elements in queue and act as a key for priority
priority_queue<pair<int,int> > p;
public:
Queue():count(0){}//initialising count to 0
//function to insert element in queue
void push(int data){
count++;
p.push(pr(count,data));
}
//function to delete element from queue
void pop(){
if(p.empty()){
cout<<"Stack is empty !!";
}
else{
count--;
p.pop();
}
}
//function to find top element
int top(){
if(p.empty()){
return INT_MAX;
}
else{
pr t =p.top();
return t.second;
}
}
};
int main(){
int element,choice,temp;
cout<<"Enter choice :"<<endl;
cout<<"1-push 2-pop 3-peek"<<endl;
cin>>choice;//choice is used to store the value of next function to pe performed
Queue *q=new Queue();
while(choice!=0)
{
if(choice==1)//insert element
{
cout<<"Enter Element :";
cin>>element;
q->push(element);
}
else if(choice==2)//delete element
{
q->pop();
}
else if(choice == 3)//find topmost element
{
if(q->top()==INT_MAX)
cout<<"Stack is empty!!"<<endl;
else
cout<<q->top()<<endl;
}
else if(choice == 0)//terminate the loop
{
break;
}
else
{
cout<<"Invalid Choice"<<endl;
}
cout<<"Enter choice :"<<endl;
cout<<"0-To Exit 1-push 2-pop 3-peek"<<endl;
cin>>choice;
}
return 0;
}