본문 바로가기
온라인 저지/BOJ

[BOJ/Java] 18258 큐2, 10866 덱

by ahj 2022. 2. 13.

거의 똑같은 문제 각각 Queue, Deque 사용을 연습할 수 있는 문제

 

18258 큐2

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.StringTokenizer;

public class BOJ18258 {

	static BufferedReader br;
	static StringBuilder sb;
	static LinkedList<String> q;
	static StringTokenizer st;
	static String tmp;
	static int n, i;

	public static void main(String[] args) throws Exception {
		br = new BufferedReader(new InputStreamReader(System.in));
		sb = new StringBuilder();
		q = new LinkedList<>();
		n = Integer.parseInt(br.readLine());
		for (i = 0; i < n; i++) {
			st = new StringTokenizer(br.readLine());
			switch (st.nextToken()) {
			case "push":
				q.add(st.nextToken());
				break;
			case "pop":
				tmp = q.poll();
				if (tmp == null)
					sb.append("-1\n");
				else
					sb.append(tmp).append("\n");
				break;
			case "size":
				sb.append(q.size()).append("\n");
				break;
			case "empty":
				if (q.isEmpty())
					sb.append("1\n");
				else
					sb.append("0\n");
				break;
			case "front":
				tmp = q.peek();
				if (tmp == null)
					sb.append("-1\n");
				else
					sb.append(tmp).append("\n");
				break;
			case "back":
				tmp = q.peekLast();
				if (tmp == null)
					sb.append("-1\n");
				else
					sb.append(tmp).append("\n");
				break;
			}
		}
		System.out.println(sb);
	}
}

10866 덱

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.StringTokenizer;

public class BOJ10866 {

	static BufferedReader br;
	static StringBuilder sb;
	static LinkedList<String> q;
	static StringTokenizer st;
	static String tmp;
	static int n, i;

	public static void main(String[] args) throws Exception {
		br = new BufferedReader(new InputStreamReader(System.in));
		sb = new StringBuilder();
		q = new LinkedList<>();
		n = Integer.parseInt(br.readLine());
		for (i = 0; i < n; i++) {
			st = new StringTokenizer(br.readLine());
			switch (st.nextToken()) {
			case "push_front":
				q.addFirst(st.nextToken());
				break;
			case "push_back":
				q.add(st.nextToken());
				break;
			case "pop_front":
				tmp = q.poll();
				if (tmp == null)
					sb.append("-1\n");
				else
					sb.append(tmp).append("\n");
				break;
			case "pop_back":
				tmp = q.pollLast();
				if (tmp == null)
					sb.append("-1\n");
				else
					sb.append(tmp).append("\n");
				break;
			case "size":
				sb.append(q.size()).append("\n");
				break;
			case "empty":
				if (q.isEmpty())
					sb.append("1\n");
				else
					sb.append("0\n");
				break;
			case "front":
				tmp = q.peek();
				if (tmp == null)
					sb.append("-1\n");
				else
					sb.append(tmp).append("\n");
				break;
			case "back":
				tmp = q.peekLast();
				if (tmp == null)
					sb.append("-1\n");
				else
					sb.append(tmp).append("\n");
				break;
			}
		}
		System.out.println(sb);
	}
}

'온라인 저지 > BOJ' 카테고리의 다른 글

[BOJ/Java] 5430 AC  (0) 2022.02.13
[BOJ/Java] 1874 스택 수열  (0) 2022.02.13
[BOJ/Java] 11047 동전 0  (0) 2022.02.13
[BOJ/Java] 1436 영화감독 숌  (0) 2022.02.13
[BOJ/Java] 1406 에디터  (0) 2022.02.13

댓글