정수를 저장하는 덱을 구현한 다음, 입력으로 주어지는 명령을 처리하는 프로그램을 작성하시오.
명령은 총 여덟 가지이다.
입력 : 첫째 줄에 명령의 수 N이 주어진다. (1 ≤ N ≤ 1,000,000)
둘째 줄부터 N개 줄에 명령이 하나씩 주어진다.
출력을 요구하는 명령은 하나 이상 주어진다.
출력 : 출력을 요구하는 명령이 주어질 때마다 명령의 결과를 한 줄에 하나씩 출력한다.

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));
int n = Integer.parseInt(br.readLine());
Deque<Integer> q = new LinkedList();
StringBuilder sb = new StringBuilder();
for(int i = 0; i<n; i++) {
StringTokenizer stz = new StringTokenizer(br.readLine());
int command = Integer.parseInt(stz.nextToken());
if(command==1) {
int pushNum = Integer.parseInt(stz.nextToken());
q.push(pushNum);
} else if(command==2) {
int pushNum = Integer.parseInt(stz.nextToken());
q.addLast(pushNum);
} else if(command==3) {
if(q.isEmpty()) {
sb.append(-1).append('\n');
} else sb.append(q.pollFirst()).append('\n');
} else if(command==4) {
if(q.isEmpty()) {
sb.append(-1).append('\n');
} else sb.append(q.pollLast()).append('\n');
} else if(command==5) {
sb.append(q.size()).append('\n');
} else if(command==6) {
if(q.isEmpty()) {
sb.append(1).append('\n');
} else sb.append(0).append('\n');
} else if(command==7) {
if(q.isEmpty()) {
sb.append(-1).append('\n');
} else sb.append(q.peek()).append('\n');
} else if(command==8) {
if(q.isEmpty()) {
sb.append(-1).append("\n");
} else sb.append(q.peekLast()).append("\n");
}
}
System.out.println(sb);
}
}