-
Notifications
You must be signed in to change notification settings - Fork 0
/
10828.cpp
52 lines (45 loc) · 876 Bytes
/
10828.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
#include <iostream>
#include <stack>
#include <string>
#include <vector>
int main(void) {
// empty stack
std::stack<int> s;
// a vector that includes results after each commands
std::vector<int> vec;
// the number of commands
int count;
std::cin >> count;
int element; // in case of insertion
// menu
std::string str;
int i;
for (i = 0; i < count; i++) {
std::cin >> str;
if (str == "push") {
std::cin >> element;
s.push(element);
}
else if (str == "top") {
if (s.empty())
vec.push_back(-1);
else
vec.push_back(s.top());
}
else if (str == "pop") {
if (s.empty())
vec.push_back(-1);
else {
vec.push_back(s.top());
s.pop();
}
}
else if (str == "size")
vec.push_back(s.size());
else
vec.push_back(s.empty());
}
for (i = 0; i < vec.size(); i++)
std::cout << vec[i] << std::endl;
return 0;
}