-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstackUsingHeap.cpp
More file actions
71 lines (54 loc) · 1.1 KB
/
stackUsingHeap.cpp
File metadata and controls
71 lines (54 loc) · 1.1 KB
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
/*
Implement a stack API using a heap
*/
#include <iostream>
#include <queue> //has priority_queue
#include <vector>
#include <utility>
using namespace std;
template <typename T>
class Compare {
public:
bool operator()(const pair<int,T> & a, const pair<int,T> & b) const{
return a.first < b.first;
}
};
//Note: MyStack is derived from STL prioity_queue
template <typename T>
class MyStack :
public priority_queue<pair<int,T> , vector<pair<int,T>>, Compare<T> > {
private:
typedef priority_queue<pair<int,T> , vector<pair<int,T>>, Compare<T> > PQ;
int count;
public:
MyStack(){
count = 0;
}
const T top() const {
T ret = PQ::top().second;
return ret;
}
void push(T data) {
PQ::emplace(count++,data);
}
//Base class functionality
/*void pop() {
pq.pop();
--count;
}
bool empty() {
pq.empty();
}
*/
};
int main(){
MyStack<int> s;
s.push(2);
s.push(4);
s.push(11);
s.push(7);
cout<<s.top()<<endl; s.pop(); //7
s.push(8);
cout<<s.top()<<endl;s.pop();//8
cout<<s.top()<<endl;//11
}