Skip to content

Commit c5a038e

Browse files
authored
[백준 10866] 덱 - 자료구조
1 parent 78dae0f commit c5a038e

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

jaehong/week02/덱.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
pop_front: 덱의 가장 앞에 있는 수를 빼고, 그 수를 출력한다. 만약, 덱에 들어있는 정수가 없는 경우에는 -1을 출력한다.
3+
pop_back: 덱의 가장 뒤에 있는 수를 빼고, 그 수를 출력한다. 만약, 덱에 들어있는 정수가 없는 경우에는 -1을 출력한다.
4+
size: 덱에 들어있는 정수의 개수를 출력한다.
5+
empty: 덱이 비어있으면 1을, 아니면 0을 출력한다.
6+
front: 덱의 가장 앞에 있는 정수를 출력한다. 만약 덱에 들어있는 정수가 없는 경우에는 -1을 출력한다.
7+
back: 덱의 가장 뒤에 있는 정수를 출력한다. 만약 덱에 들어있는 정수가 없는 경우에는 -1을 출력한다.
8+
*/
9+
10+
import java.io.*;
11+
import java.util.*;
12+
public class Boj10866{
13+
14+
public static void main(String...args)throws Exception{
15+
var br = new BufferedReader(new InputStreamReader(System.in));
16+
var sb = new StringBuilder();
17+
var deque = new ArrayDeque<Integer>();
18+
19+
var numberOfLine = Integer.parseInt(br.readLine());
20+
21+
while(numberOfLine-- > 0){
22+
var st = new StringTokenizer(br.readLine());
23+
var command = st.nextToken();
24+
25+
if("push_back".equals(command)){
26+
deque.offerLast(Integer.parseInt(st.nextToken()));
27+
continue;
28+
}
29+
30+
if("push_front".equals(command)){
31+
deque.offerFirst(Integer.parseInt(st.nextToken()));
32+
continue;
33+
}
34+
35+
36+
var result = switch(command){
37+
case "pop_front" -> deque.isEmpty() ? -1 : deque.pollFirst();
38+
case "pop_back" -> deque.isEmpty() ? -1 : deque.pollLast();
39+
case "size" -> deque.size();
40+
case "empty" -> deque.isEmpty() ? 1 : 0;
41+
case "front" -> deque.isEmpty() ? -1 : deque.peekFirst();
42+
case "back" -> deque.isEmpty() ? -1 : deque.peekLast();
43+
default -> throw new IllegalArgumentException();
44+
};
45+
46+
sb.append(result).append("\n");
47+
}
48+
System.out.print(sb);
49+
}
50+
}

0 commit comments

Comments
 (0)