-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimuStack.java
More file actions
27 lines (25 loc) · 778 Bytes
/
SimuStack.java
File metadata and controls
27 lines (25 loc) · 778 Bytes
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
import java.util.Scanner;
import java.util.Stack;
public class SimuStack {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int M = sc.nextInt();
Stack<Integer> stk = new Stack<>();
for (int i = 0; i < M; i++){
String op = sc.next();
if (op.equals("push")){
int x = sc.nextInt();
stk.push(x);
} else if (op.equals("pop")) {
stk.pop();
} else if (op.equals("empty")) {
if (stk.empty())
System.out.println("YES");
else
System.out.println("NO");
} else {
System.out.println(stk.peek());
}
}
}
}