-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.cpp
More file actions
54 lines (44 loc) · 1.07 KB
/
stack.cpp
File metadata and controls
54 lines (44 loc) · 1.07 KB
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
#include <iostream>
#include <vector>
using namespace std;
void execute(string input, int index);
vector<int> stack;
vector<string> command_arr;
int main(void){
int i, n;
string command;
cin >> n;
for(i = 0; i < n; i++){
cin >> command;
command_arr.push_back(command);
if(command == "push"){
i--;
}
}
for(i = 0; i < command_arr.size(); i++){
execute(command_arr[i], i);
}
return 0;
}
void execute(string input, int i){
if(input == "push"){
stack.push_back(stoi(command_arr[i+1]));
} else if(input == "pop"){
if(!(stack.empty())){
cout << stack.back() << endl;
stack.pop_back();
} else {
cout << -1 << endl;
}
} else if(input == "size"){
cout << stack.size() << endl;
} else if(input == "empty"){
cout << stack.empty() << endl;
} else if(input == "top"){
if(!(stack.empty())){
cout << stack.back() << endl;
} else {
cout << -1 << endl;
}
}
}