-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path18258
58 lines (55 loc) · 1.35 KB
/
18258
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
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
// StringBuilder sb = new StringBuilder();
int N = Integer.parseInt(br.readLine());
int temp = -1;
Queue<Integer> q = new LinkedList<>();
for (int i = 0; i < N; i++) {
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
switch (st.nextToken()) {
case "push":
temp = Integer.parseInt(st.nextToken());
q.add(temp);
break;
case "pop":
if (q.isEmpty())
bw.write(-1 + "\n");
else
bw.write(q.poll() + "\n");
break;
case "size":
bw.write(q.size() + "\n");
break;
case "empty":
if (q.isEmpty())
bw.write(1 + "\n");
else
bw.write(0 + "\n");
break;
case "front":
if (q.isEmpty())
bw.write(-1 + "\n");
else
bw.write(q.peek() + "\n");
break;
case "back":
if (q.isEmpty())
bw.write(-1 + "\n");
else
bw.write(temp + "\n");
}
}
bw.flush();
bw.close();
}
}