📝 문제
백준 - 10845번 : 큐 (Silver IV)
📝 풀이
📌 작성 코드
import java.io.*;
import java.util.*;
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));
int t = Integer.parseInt(br.readLine());
ArrayList<String> queue = new ArrayList<>();
for (int i = 0; i < t; i++) {
String[] s = br.readLine().split(" ");
switch (s[0]) {
case "push":
queue.add(s[1]);
break;
case "pop":
if (queue.isEmpty()) {
bw.write("-1\n");
} else {
bw.write(queue.get(0) + "\n");
queue.remove(0);
}
break;
case "size":
bw.write(queue.size() + "\n");
break;
case "empty":
bw.write(queue.isEmpty() ? "1\n" : "0\n");
break;
case "front":
bw.write(queue.isEmpty() ? "-1\n" : queue.get(0) + "\n");
break;
case "back":
bw.write(queue.isEmpty() ? "-1\n" : queue.get(queue.size()-1) + "\n");
break;
}
}
bw.flush();
bw.close();
}
}
📌 결과