import java.util.*;
import java.io.*;
class Main {
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
StringTokenizer st;
Deque<Integer> deque = new LinkedList<Integer>();
int N = Integer.parseInt(br.readLine());
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
String type = st.nextToken();
switch (type) {
case "push_front":
deque.addFirst(Integer.parseInt(st.nextToken()));
break;
case "push_back":
deque.addLast(Integer.parseInt(st.nextToken()));
break;
case "pop_front":
sb.append((deque.isEmpty() ? -1 : deque.pollFirst())).append("\n");
break;
case "pop_back":
sb.append((deque.isEmpty() ? -1 : deque.pollLast())).append("\n");
break;
case "size":
sb.append(deque.size()).append("\n");
break;
case "empty":
sb.append((deque.isEmpty() ? 1 : 0)).append("\n");
break;
case "front":
sb.append((deque.isEmpty() ? -1 : deque.peekFirst())).append("\n");
break;
case "back":
sb.append((deque.isEmpty() ? -1 : deque.peekLast())).append("\n");
break;
}
}
System.out.println(sb);
}
}
스택 , 큐랑 거의 동일한 문제 하도 풀다보니 메서드 이름도 외워졌다.