-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJ10828.java
More file actions
39 lines (36 loc) · 1.3 KB
/
Copy pathJ10828.java
File metadata and controls
39 lines (36 loc) · 1.3 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
package TIL;
import java.io.*;
import java.util.*;
public class J10828 {
public static void main(String[] args) throws IOException {
BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
Deque<Integer> stack = new ArrayDeque<>();
int num = Integer.parseInt(buffer.readLine());
for(int i = 0 ; i < num; i++) {
String[] input = buffer.readLine().split(" ");
String value = input[0];
switch(value){
case "push":
stack.push(Integer.parseInt(input[1]));
break;
case "pop":
if(stack.size() ==0){
System.out.println(-1);
}
else System.out.println(stack.pop());
break;
case "size":
System.out.println(stack.size());
break;
case "top":
if(stack.peek() == null) System.out.println(-1);
else System.out.println(stack.peek());
break;
case "empty":
if(stack.isEmpty()) System.out.println(1);
else System.out.println(0);
break;
}
}
}
}