-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path큐 2.java
More file actions
50 lines (43 loc) · 1.67 KB
/
큐 2.java
File metadata and controls
50 lines (43 loc) · 1.67 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
50
// 큐 2
import java.util.*;
import java.lang.*;
import java.io.*;
class Main {
public static ArrayDeque<Integer> queue = new ArrayDeque<>();
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
StringTokenizer st;
int n = Integer.parseInt(br.readLine());
for (int i = 0; i < n; i++) {
st = new StringTokenizer(br.readLine());
String command = st.nextToken();
if (command.equals("push")) {
int tmp = Integer.parseInt(st.nextToken());
queue.offer(tmp);
} else if (command.equals("pop")) {
if (queue.isEmpty())
sb.append(-1).append('\n');
else {
sb.append(queue.poll()).append('\n');
}
} else if (command.equals("size")) {
sb.append(queue.size()).append('\n');
} else if (command.equals("empty")) {
sb.append(queue.isEmpty() ? 1 : 0).append('\n');
} else if (command.equals("front")) {
if (queue.isEmpty())
sb.append(-1).append('\n');
else
sb.append(queue.peek()).append('\n');
} else if (command.equals("back")) {
if (queue.isEmpty()) {
sb.append(-1).append('\n');
} else {
sb.append(queue.peekLast()).append('\n');
}
}
}
System.out.println(sb);
}
}