-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBJAlgo_10828.java
More file actions
49 lines (45 loc) · 1.62 KB
/
Copy pathBJAlgo_10828.java
File metadata and controls
49 lines (45 loc) · 1.62 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
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class BJAlgo_10828 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int count = Integer.parseInt(br.readLine());
List stack = new ArrayList();
for(int i = 0; i < count; i++) {
String[] comm = br.readLine().split(" ");
switch (comm[0]) {
case "push":
stack.add(Integer.parseInt(comm[1]));
break;
case "pop":
if (stack.size() > 0) {
int data = (int) stack.get(stack.size() - 1);
stack.remove(stack.size() - 1);
System.out.println(data);
} else {
System.out.println(-1);
}
break;
case "size":
System.out.println(stack.size());
break;
case "empty":
if (stack.isEmpty()) {
System.out.println(1);
} else {
System.out.println(0);
}
break;
case "top":
if (stack.size() > 0) {
System.out.println(stack.get(stack.size() - 1));
} else {
System.out.println(-1);
}
break;
}
}
}
}