-
Notifications
You must be signed in to change notification settings - Fork 0
/
10828.java
71 lines (61 loc) · 1.24 KB
/
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
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;
class DequeClass {
Stack<Integer> stack;
public DequeClass() {
stack = new Stack<>();
}
public void push(int x) {
stack.push(x);
}
public int pop() {
if (size() == 0) {
return -1;
} else {
return stack.pop();
}
}
public int size() {
return stack.size();
}
public int empty() {
if (stack.isEmpty() == true) {
return 1;
} else {
return 0;
}
}
public int top() {
if(size() == 0) {
return -1;
}
return stack.peek();
}
}
public class Main {
public static void main(String[] args) throws IOException {
DequeClass dec = new DequeClass();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
for (int i = 0; i < n; i++) {
String[] s = br.readLine().split(" ");
if (s[0].equals("push")) {
dec.push(Integer.parseInt(s[1]));
}
if (s[0].equals("pop")) {
System.out.println(dec.pop());
}
if (s[0].equals("empty")) {
System.out.println(dec.empty());
}
if (s[0].equals("size")) {
System.out.println(dec.size());
}
if (s[0].equals("top")) {
System.out.println(dec.top());
}
}
}
}