-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10828.java
27 lines (23 loc) · 883 Bytes
/
10828.java
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
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
Stack<Integer> stack = new Stack<Integer>();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int num = Integer.parseInt(br.readLine());
for(int i=0; i<num; i++) {
String[] input = br.readLine().split(" ");
if(input[0].contains("push")) {
stack.push(Integer.parseInt(input[1]));
}else if(input[0].equals("pop")){
System.out.println(stack.isEmpty()?-1:stack.pop());
}else if(input[0].equals("size")) {
System.out.println(stack.size());
}else if(input[0].equals("empty")) {
System.out.println(stack.isEmpty()?1:0);
}else if(input[0].equals("top")) {
System.out.println(stack.isEmpty()?-1:stack.peek());
}
}
}
}