-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaxstackcpy.cpp
More file actions
51 lines (44 loc) · 803 Bytes
/
Copy pathmaxstackcpy.cpp
File metadata and controls
51 lines (44 loc) · 803 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
#include <iostream>
using namespace std;
struct stack {
struct s_node {
int value;
int max_value;
s_node *prev;
};
s_node *top = nullptr;
void push(int value) {
top = new s_node { value, top ? max(top->max_value, value) : value, top };
}
void pop() {
if (top)
top = top->prev;
}
int max_value() {
if (!top)
throw exception();
return top->max_value;
}
};
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
stack s;
int n; cin >> n;
while (n--) {
int o; cin >> o;
switch (o) {
case 1: {
int v; cin >> v;
s.push(v);
}
break;
case 2:
s.pop();
break;
case 3:
cout << s.max_value() << '\n';
break;
}
}
return 0;
}