[BOJ 10866] 덱 (java)
문제 링크 https://www.acmicpc.net/problem/10866
문제 풀이
package basics_200;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.Deque;
public class BOJ10866 {
public static void main(String[] args) throws NumberFormatException, IOException {
// TODO Auto-generated method stub
//자바에서의 덱 선언
Deque<Integer> deque = new ArrayDeque<>();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// 테스트케이스
Integer tc = Integer.parseInt(br.readLine());
while(tc>0) {
String s = br.readLine();
String[] array = s.split(" ");
switch(array[0]) {
case "push_front":
deque.offerFirst(Integer.parseInt(array[1]));
break;
case "push_back":
deque.offerLast(Integer.parseInt(array[1]));
break;
case "pop_front":
if(!deque.isEmpty()) {
System.out.println(deque.pollFirst());
}else {
System.out.println(-1);
}
break;
case "pop_back":
if(!deque.isEmpty()) {
System.out.println(deque.pollLast());
}else {
System.out.println(-1);
}
break;
case "size":
System.out.println(deque.size());
break;
case "empty":
if(!deque.isEmpty()) {
System.out.println(0);
}else {
System.out.println(1);
}
break;
case "front":
if(!deque.isEmpty()) {
System.out.println(deque.peekFirst());
}else {
System.out.println(-1);
}
break;
case "back":
if(!deque.isEmpty()) {
System.out.println(deque.peekLast());
}else {
System.out.println(-1);
}
break;
}
tc--;
}
}
}
Comments