728x90
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
try {
int N = Integer.parseInt(bf.readLine());
Queue<Integer> queue = new LinkedList<>();
StringBuilder sb = new StringBuilder();
int last = 0;
for(int i = 0 ; i < N; i++) {
StringTokenizer st = new StringTokenizer(bf.readLine());
switch (st.nextToken()){
case "push": {
last = Integer.parseInt(st.nextToken());
queue.add(last);
break;
}case "front" :{
if(queue.isEmpty()) sb.append(-1).append("\n");
else sb.append(queue.peek()).append("\n");
break;
}case "back" :{
if(queue.isEmpty()) sb.append(-1).append("\n");
else sb.append(last).append("\n");
break;
}case "empty" :{
if(queue.isEmpty()) sb.append(1).append("\n");
else sb.append(0).append("\n");
break;
}case "size" :{
sb.append(queue.size()).append("\n");
break;
}case "pop":{
if(queue.isEmpty()) sb.append(-1).append("\n");
else sb.append(queue.poll()).append("\n");
break;
}
}
}
System.out.println(sb);
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
728x90
'PS > BaekJoon' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
1259 JAVA ํฐ๋ฆฐ๋๋กฌ์ (0) | 2022.08.31 |
---|---|
10828 JAVA ์คํ (0) | 2022.08.30 |
2609 JAVA ์ต๋๊ณต์ฝ์์ ์ต์๊ณต๋ฐฐ์ (0) | 2022.08.26 |
11650 JAVA ์ขํ ์ ๋ ฌํ๊ธฐ (0) | 2022.07.12 |
1427 JAVA ์ํธ์ธ์ฌ์ด๋ (0) | 2022.07.08 |