https://www.acmicpc.net/problem/10866
10866번: 덱
첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지
www.acmicpc.net
📌 작성한 코드
// 10866
const fs = require("fs");
const filePath = process.platform === "linux" ? "/dev/stdin" : "Beakjoon/Silver/test.txt";
let input = fs.readFileSync(filePath).toString().trim().split("\n");
class Node {
constructor(value) {
this.value = value;
this.next = null;
this.prev = null;
}
}
class Deque {
constructor() {
this.count = 0;
this.front = null;
this.rear = null;
}
unshift(value) {
const node = new Node(value);
if (this.count === 0) {
this.front = node;
this.rear = node;
} else {
const prevFront = this.front;
prevFront.prev = node;
this.front = node;
node.next = prevFront;
}
this.count += 1;
}
shift() {
if (this.count === 0) return null;
const value = this.front.value;
if (this.count === 1) {
this.count = 0;
this.front = null;
this.rear = null;
} else {
this.front = this.front.next;
this.front.prev = null;
this.count -= 1;
}
return value;
}
push(value) {
const node = new Node(value);
if (this.count === 0) {
this.front = node;
this.rear = node;
} else {
const prevRear = this.rear;
prevRear.next = node;
node.prev = prevRear;
this.rear = node;
}
this.count += 1;
}
pop() {
if (this.count === 0) return null;
const value = this.rear.value;
if (this.count === 1) {
this.count = 0;
this.front = null;
this.rear = null;
} else {
this.rear = this.rear.prev;
this.rear.next = null;
this.count -= 1;
}
return value;
}
empty() {
return this.count === 0 ? 1 : 0;
}
getFront() {
return this.front ? this.front.value : null;
}
getBack() {
return this.rear ? this.rear.value : null;
}
}
const N = Number(input[0]);
const deque = new Deque();
const answer = [];
for (let i = 1; i <= N; i++) {
const [command, num] = input[i].split(" ");
if (command === "push_front") deque.unshift(Number(num));
if (command === "pop_front") answer.push(deque.shift() || -1);
if (command === "push_back") deque.push(Number(num));
if (command === "pop_back") answer.push(deque.pop() || -1);
if (command === "size") answer.push(deque.count);
if (command === "empty") answer.push(deque.empty());
if (command === "front") answer.push(deque.getFront() || -1);
if (command === "back") answer.push(deque.getBack() || -1);
}
console.log(answer.join("\n"));
✅ 성공

'알고리즘 > 백준' 카테고리의 다른 글
| [JavaScript] 백준 골드 5 : 5430 - AC (1) | 2024.01.23 |
|---|---|
| [JavaScript/Queue] 백준 실버 3 : 1021 - 회전하는 큐 (0) | 2024.01.23 |
| [JavaScript/Queue] 백준 실버 4 : 2164 - 카드 2 (0) | 2024.01.22 |
| [JavaScript/Stack] 백준 골드 4 : 17298 - 오큰수 (0) | 2024.01.22 |
| [JavaScript] 백준 골드 5 : 6198 - 옥상 정원 꾸미기 (0) | 2024.01.22 |