Baekjoon - 10845

Tadap·2023년 9월 4일
0

Baekjoon

목록 보기
5/94

문제

Solved.ac CLASS 2⁺

1차시도

public class Main {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int size = Integer.parseInt(br.readLine());
		StringBuilder sb = new StringBuilder();
		Queue<String> queue = new LinkedList<>();
		String lastVal = null;


		for (int i = 0; i < size; i++) {
			String[] split = br.readLine().split(" ");
			if (split[0].equals("push")) {
				queue.add(split[1]);
				lastVal = split[1];
			} else if (split[0].equals("size")) {
				sb.append(queue.size()).append("\n");
			} else if (split[0].equals("empty")) {
				if (queue.isEmpty()) {
					sb.append("1").append("\n");
				}else{
					sb.append("0").append("\n");
				}
			} else if (split[0].equals("front")) {
				if (queue.isEmpty()) {
					sb.append("-1").append("\n");
				}else{
					sb.append(queue.peek()).append("\n");
				}
			} else if (split[0].equals("back")) {
				if (queue.isEmpty()) {
					sb.append("-1").append("\n");
				} else {
					sb.append(lastVal).append("\n");
				}
			} else if (split[0].equals("pop")) {
				if (queue.isEmpty()) {
					sb.append("-1").append("\n");
				} else {
					sb.append(queue.poll()).append("\n");
				}
			}
		}
		System.out.println(sb);
	}
}

back은 매서드가 없어서 그냥 lastVal에 저장했다.

성공

0개의 댓글