📍 문제 설명
💡 접근
덱 기초문제
👩💻 코드
import java.util.*;
public class Main {
static int n;
public static void main(String[] args) {
Main T = new Main();
Scanner kb = new Scanner(System.in);
n = kb.nextInt();
kb.nextLine();
Deque<Integer> dq = new ArrayDeque<>();
StringBuilder sb = new StringBuilder();
for(int i = 0; i < n; i++) {
String[] s = kb.nextLine().split(" ");
switch (s[0]) {
case "push_front" : {
dq.addFirst(Integer.parseInt(s[1]));
break;
}
case "push_back" : {
dq.addLast(Integer.parseInt(s[1]));
break;
}
case "pop_front" : {
if(!dq.isEmpty()) sb.append(dq.pollFirst()).append("\n");
else sb.append(-1).append("\n");
break;
}
case "pop_back" : {
if(!dq.isEmpty()) sb.append(dq.pollLast()).append("\n");
else sb.append(-1).append("\n");
break;
}
case "size" : {
sb.append(dq.size()).append("\n");
break;
}
case "empty" : {
if(dq.isEmpty()) sb.append(1).append("\n");
else sb.append(0).append("\n");
break;
}
case "front" : {
if(!dq.isEmpty()) sb.append(dq.peekFirst()).append("\n");
else sb.append(-1).append("\n");
break;
}
case "back" : {
if(!dq.isEmpty()) sb.append(dq.peekLast()).append("\n");
else sb.append(-1).append("\n");
break;
}
}
}
System.out.println(sb);
}
private void solution() {
}
}