풀이방법
사용된 것:
Deque
2022.04.13
Switch-case문으로 구현하면 된다.
코드
Java(2022.04.13)
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{
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Deque<Integer> deque = new LinkedList<Integer>();
int n = Integer.parseInt(br.readLine());
StringBuilder sb = new StringBuilder();
for(int i = 0; i<n; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
String command = st.nextToken();
switch(command) {
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());
sb.append(System.lineSeparator()); break;
case "pop_back" : sb.append(deque.isEmpty()? -1 : deque.pollLast());
sb.append(System.lineSeparator()); break;
case "size" : sb.append(deque.size()); sb.append(System.lineSeparator()); break;
case "empty" : sb.append(deque.isEmpty()? 1 : 0);
sb.append(System.lineSeparator()); break;
case "front" : sb.append(deque.isEmpty()? -1 : deque.peekFirst());
sb.append(System.lineSeparator()); break;
case "back" : sb.append(deque.isEmpty()? -1 : deque.peekLast());
sb.append(System.lineSeparator()); break;
}
}
System.out.print(sb);
}
}
'알고리즘 문제풀이 > 백준' 카테고리의 다른 글
#1966 : 프린터 큐 (0) | 2022.04.13 |
---|---|
#11650 : 좌표 정렬하기 (0) | 2022.04.13 |
#10845 : 큐 (0) | 2022.04.13 |
#10989 : 수 정렬하기 3 (0) | 2022.04.13 |
#10828 : 스택 (0) | 2022.04.12 |