-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminStack.c
More file actions
57 lines (47 loc) · 821 Bytes
/
Copy pathminStack.c
File metadata and controls
57 lines (47 loc) · 821 Bytes
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
#include "util.h"
template<class T>
class MinStack
{
public:
//入栈函数
void push(const T &t){
datas.push(t);
if(minStack.size()==0)
minStack.push(t);
else if(t<minStack.top())
{
minStack.push(t);
}
else
{
minStack.push(minStack.top());
}
}
//出栈函数
void pop(){
if(datas.size()!=0&&minStack.size()!=0)
{
datas.pop();
minStack.pop();
}
}
//获取最小值的函数
const T& min(){
return minStack.top();
}
private:
std::stack<T> datas;
std::stack<T> minStack;
};
int main(){
MinStack<int> s;
s.push(3);
s.push(4);
s.push(2);
s.push(1);
std::cout<<"包含min函数的栈"<<std::endl;
std::cout<<s.min()<<std::endl;
s.pop();
std::cout<<s.min()<<std::endl;
return 0;
}