-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetminelementfromstack.cpp
83 lines (77 loc) · 1.24 KB
/
getminelementfromstack.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
#include<bits/stdc++.h>
using namespace std;
class _stack{
stack<int> s;
int minEle;
public :
int getMin();
int pop();
void push(int);
};
int main()
{
int t;
cin>>t;
while(t--)
{
int q;
cin>>q;
_stack *a = new _stack();
while(q--){
int qt;
cin>>qt;
if(qt==1)
{
//push
int att;
cin>>att;
a->push(att);
}
else if(qt==2)
{
//pop
cout<<a->pop()<<" ";
}
else if(qt==3)
{
//getMin
cout<<a->getMin()<<" ";
}
}
cout<<endl;
}
}
}
/*This is a function problem.You only need to complete the function given below*/
/*
The structure of the class is as follows
class _stack{
stack<int> s;
int minEle;
public :
int getMin();
int pop();
void push(int);
};*/
int _stack :: getMin()
{
if(s.empty()) return -1;
return s.top();
}
int _stack ::pop()
{
if(s.empty()) return -1;
s.pop();
int o = s.top();
s.pop();
if(!s.empty())minEle = s.top();
return o;
}
/*push element x into the stack*/
void _stack::push(int x)
{
if(s.empty()) minEle=INT_MAX;
if(x < minEle) minEle = x;
s.push(x);
s.push(minEle);
}