문제
정수를 저장하는 덱(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보다 작거나 같다. 문제에 나와있지 않은 명령이 주어지는 경우는 없다.
출력
출력해야하는 명령이 주어질 때마다, 한 줄에 하나씩 출력한다.
입출력 예시
입력
15
push_back 1
push_front 2
front
back
size
empty
pop_front
pop_back
pop_front
size
empty
pop_back
push_front 3
empty
front
출력
2
1
2
0
2
1
-1
0
1
-1
0
3
풀이
'use strict';
const fs = require('fs');
// 백준 제출 할 때 주석 제거
// const readFileSyncAddress = '/dev/stdin';
// VSC 테스트 할 때 주석 제거
const readFileSyncAddress = 'input.txt';
const input = fs.readFileSync(readFileSyncAddress).toString().trim().split('\n');
const N = input.shift();
class Node {
constructor(item) {
this.item = item;
this.next = null;
this.prev = null;
}
}
class Deque {
constructor() {
this.head = null;
this.tail = null;
this.size = 0;
}
pushFront(item) {
const node = new Node(item);
if (this.empty()) {
this.tail = node;
} else {
node.next = this.head;
this.head.prev = node;
}
this.head = node;
this.size += 1;
}
pushBack(item) {
const node = new Node(item);
if (this.empty()) {
this.head = node;
} else {
node.prev = this.tail;
this.tail.next = node;
}
this.tail = node;
this.size += 1;
}
popFront() {
if (this.empty()) {
return -1;
}
const node = this.head;
this.head = this.head.next;
if (this.head !== null) this.head.prev = null;
this.size -= 1;
return node.item;
}
popBack() {
if (this.empty()) {
return -1;
}
const node = this.tail;
this.tail = this.tail.prev;
if (this.tail !== null) this.tail.next = null;
this.size -= 1;
return node.item;
}
length() {
return this.size;
}
empty() {
if (this.size == 0) {
return 1;
}
return 0;
}
front() {
if (this.empty()) {
return -1;
}
return this.head.item;
}
back() {
if (this.empty()) {
return -1;
}
return this.tail.item;
}
}
function solution(input) {
const deque = new Deque();
let answer = [];
for (let command of input) {
const c = command.split(' ');
switch (c[0]) {
case 'push_front':
deque.pushFront(c[1]);
break;
case 'push_back':
deque.pushBack(c[1]);
break;
case 'pop_front':
answer.push(deque.popFront());
break;
case 'pop_back':
answer.push(deque.popBack());
break;
case 'size':
answer.push(deque.length());
break;
case 'empty':
answer.push(deque.empty());
break;
case 'front':
answer.push(deque.front());
break;
case 'back':
answer.push(deque.back());
break;
}
}
return answer;
}
const answer = solution(input);
console.log(answer.join('\n'));
Deque(덱)은 양쪽 끝에서 데이터의 추가와 삭제가 가능한 자료구조이다. 일반적인 큐(Queue)와 달리 앞쪽과 뒤쪽에서 모두 삽입과 삭제를 수행할 수 있는 것이 특징이다.
Deque를 구현할 때는 각 노드가 다음 노드를 가리키는 next 포인터뿐만 아니라, 이전 노드를 가리키는 prev 포인터도 필요하다. 이 이중 연결 포인터를 통해 양쪽 끝에서의 삽입과 삭제를 효율적으로 처리할 수 있다.
'알고리즘' 카테고리의 다른 글
[ 백준 ] 10799 쇠막대기 - JavaScript (1) | 2025.01.07 |
---|---|
[ 백준 ] 1966 프린터 큐 - JavaScript (0) | 2025.01.07 |
[ 백준 ] 2346 풍선 터뜨리기 - JavaScript (0) | 2025.01.02 |
[ 백준 ] 2164 카드2 - JavaScript (1) | 2025.01.02 |
[ 백준 ] 1158 요세푸스 문제 - JavaScript (0) | 2025.01.02 |