forked from Dev-XYS/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stack.cpp
73 lines (64 loc) · 872 Bytes
/
Stack.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
#include <cstdio>
#include <cstring>
#define ELEMENT_COUNT 100000
using namespace std;
// Requirement: every element in stack must be positive.
// Otherwise, M[0] should be set to -INF.
int S[ELEMENT_COUNT], M[ELEMENT_COUNT];
int sp = 1;
void push(int x)
{
S[sp] = x;
if (x > M[sp - 1])
{
M[sp] = x;
}
else
{
M[sp] = M[sp - 1];
}
sp++;
}
int pop()
{
return S[--sp];
}
bool empty()
{
return sp == 1;
}
int get_max()
{
return M[sp - 1];
}
int main()
{
while (true)
{
char o[10];
int a;
scanf(" %s", o);
if (strcmp(o, "push") == 0)
{
scanf("%d", &a);
push(a);
}
else if (strcmp(o, "pop") == 0)
{
printf("%d\n", pop());
}
else if (strcmp(o, "getmax") == 0)
{
printf("%d\n", get_max());
}
else if (strcmp(o, "abort") == 0)
{
return 0;
}
else
{
printf("No such instruction.\n");
}
}
return 0;
}