728x90
문제
정수를 저장하는 덱(Deque)를 구현한 다음, 입력으로 주어지는 명령을 처리하는 프로그램을 작성하시오.
명령은 총 여덟 가지이다.
- push_front X: 정수 X를 덱의 앞에 넣는다.
- push_back X: 정수 X를 덱의 뒤에 넣는다.
- pop_front: 덱의 가장 앞에 있는 수를 빼고, 그 수를 출력한다. 만약, 덱에 들어있는 정수가 없는 경우에는 -1을 출력한다.
- pop_back: 덱의 가장 뒤에 있는 수를 빼고, 그 수를 출력한다. 만약, 덱에 들어있는 정수가 없는 경우에는 -1을 출력한다.
- size: 덱에 들어있는 정수의 개수를 출력한다.
- empty: 덱이 비어있으면 1을, 아니면 0을 출력한다.
- front: 덱의 가장 앞에 있는 정수를 출력한다. 만약 덱에 들어있는 정수가 없는 경우에는 -1을 출력한다.
- back: 덱의 가장 뒤에 있는 정수를 출력한다. 만약 덱에 들어있는 정수가 없는 경우에는 -1을 출력한다.
입력
첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지 않은 명령이 주어지는 경우는 없다.
출력
출력해야하는 명령이 주어질 때마다, 한 줄에 하나씩 출력한다.
Deque의 각 메서드를 구현하였다
package Silver.s4;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class BOJ_10866_Deque {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
MyDeque deque = new MyDeque();
int N = Integer.parseInt(br.readLine());
for (int i = 0; i < N; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
String command = st.nextToken();
switch (command) {
case "push_back":
int X = Integer.parseInt(st.nextToken());
deque.offer(X);
break;
case "push_front":
int Y = Integer.parseInt(st.nextToken());
deque.offerFirst(Y);
break;
case "pop_front":
sb.append(deque.poll());
sb.append("\n");
break;
case "pop_back":
sb.append(deque.pollLast());
sb.append("\n");
break;
case "size":
sb.append(deque.getSize());
sb.append("\n");
break;
case "empty":
if (deque.isEmpty())
sb.append(1 + "\n");
else
sb.append(0 + "\n");
break;
case "front":
sb.append(deque.peek() + "\n");
break;
case "back":
sb.append(deque.peekLast() + "\n");
break;
}
}
System.out.println(sb.toString());
}
}
class MyDeque {
private DNode<Integer> head;
private DNode<Integer> tail;
private int size;
public MyDeque() {
this.head = null;
this.tail = null;
this.size = 0;
}
public boolean isEmpty() {
return size == 0;
}
public int getSize() {
return size;
}
public void offerFirst(Integer data) {
// offerFirst
DNode<Integer> newNode = new DNode<Integer>(data);
newNode.next = head;
if (head != null)
head.prev = newNode;
head = newNode;
if (head.next == null)
tail = head;
size++;
}
public void offer(Integer data) {
if (size == 0) {
offerFirst(data);
return;
}
DNode<Integer> newNode = new DNode<Integer>(data);
newNode.prev = tail;
tail.next = newNode;
tail = newNode;
size++;
// offerLast
}
public Integer poll() {
// pollFirst
if (head == null)
return -1;
Integer data = head.data;
head = head.next;
if (head != null)
head.prev = null;
size--;
if (size == 0)
tail = null;
return data;
}
public Integer pollLast() {
if (tail == null)
return -1;
Integer data = tail.data;
tail = tail.prev;
if (tail != null)
tail.next = null;
size--;
if (size == 0)
head = null;
return data;
}
public Integer peek() {
if (head == null)
return -1;
return head.data;
}
public Integer peekLast() {
if (tail == null)
return -1;
return tail.data;
}
}
class DNode<E> {
E data;
DNode<E> prev;
DNode<E> next;
public DNode(E data) {
this.data = data;
this.prev = null;
this.next = null;
}
public DNode(E data, DNode<E> prev, DNode<E> next) {
this.data = data;
this.prev = prev;
this.next = next;
}
}
728x90
'문제 풀이 > BOJ' 카테고리의 다른 글
[백준] 2116 - 주사위 쌓기 (자바/Java) (0) | 2022.08.23 |
---|---|
[백준] 17276 배열 돌리기 (자바/Java) (0) | 2022.08.22 |
[백준] 1966 - 프린터 큐 (자바/Java) (0) | 2022.08.18 |
[백준] 17298 - 오큰수 (자바/Java) (0) | 2022.08.18 |
[백준] 1874 - 스택수열 (자바/Java) (0) | 2022.08.18 |