-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMain.java
48 lines (40 loc) Β· 1.44 KB
/
Main.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
package Queue.P18258;
import java.io.*;
import java.util.*;
public class Main {
static int N, rear;
static Queue<Integer> q;
public static void main(String[] args) throws Exception {
System.setIn(new FileInputStream("src/Queue/P18258/input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
q = new LinkedList<>();
StringBuilder sb = new StringBuilder();
while (N-- > 0) {
StringTokenizer st = new StringTokenizer(br.readLine());
String cmd = st.nextToken();
switch (cmd) {
case "push":
rear = Integer.parseInt(st.nextToken());
q.offer(rear);
break;
case "pop":
sb.append(q.isEmpty() ? -1 : q.poll()).append("\n");
break;
case "size":
sb.append(q.size()).append("\n");
break;
case "empty":
sb.append(q.isEmpty() ? 1 : 0).append("\n");
break;
case "front":
sb.append(q.isEmpty() ? -1 : q.peek()).append("\n");
break;
case "back":
sb.append(q.isEmpty() ? -1 : rear).append("\n");
break;
}
}
System.out.print(sb);
}
}