https://www.acmicpc.net/problem/10866
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Deque;
import java.util.LinkedList;
import java.util.StringTokenizer;
public class Main{
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int n = Integer.parseInt(br.readLine());
Deque<Integer> deque = new LinkedList<>();
for(int i=0; i<n; i++){
StringTokenizer st = new StringTokenizer(br.readLine());
String str = st.nextToken();
switch(str){
case "push_front":
deque.addFirst(Integer.parseInt(st.nextToken()));
break;
case "push_back":
deque.addLast(Integer.parseInt(st.nextToken()));
break;
case "pop_front":
if(deque.isEmpty()) sb.append(-1).append("\n");
else sb.append(deque.removeFirst()).append("\n");
break;
case "pop_back":
if(deque.isEmpty()) sb.append(-1).append("\n");
else sb.append(deque.removeLast()).append("\n");
break;
case "size":
sb.append(deque.size()).append("\n");
break;
case "empty":
if(deque.isEmpty()) sb.append(1).append("\n");
else sb.append(0).append("\n");
break;
case "front":
if(deque.isEmpty()) sb.append(-1).append("\n");
else sb.append(deque.peekFirst()).append("\n");
break;
case "back":
if(deque.isEmpty()) sb.append(-1).append("\n");
else sb.append(deque.peekLast()).append("\n");
break;
}
}
System.out.println(sb);
}
}
'ALGORITHM' 카테고리의 다른 글
[JAVA] 백준 17298번- 오큰수 (0) | 2022.10.09 |
---|---|
[JAVA] 백준 1406번- 에디터 (0) | 2022.10.07 |
[JAVA] 백준 10845번- 큐 (0) | 2022.10.05 |
[JAVA] 백준 9093번- 단어 뒤집기 (0) | 2022.10.04 |
[JAVA] 백준 10828번- 스택 (0) | 2022.10.04 |