문제 링크
풀이
const input = require("fs").readFileSync("/dev/stdin").toString().trim().split("\n");
class Queue {
constructor() {
this.head = null;
this.tail = null;
this.size = 0;
}
createNode(value, prev, next) {
return {
value,
prev,
next
};
}
push(value) {
const curNode = this.createNode(value, this.tail, this.head);
if (this.head) {
this.tail.next = curNode;
this.head.prev = curNode;
this.tail = curNode;
} else {
this.head = curNode;
this.tail = curNode;
curNode.prev = curNode;
curNode.next = curNode;
}
this.size++;
}
pop() {
if (this.size > 2) {
const value = this.head.value;
const newHead = this.head.next;
this.head = newHead;
newHead.prev = this.tail;
this.tail.next = this.head;
this.size--;
return value;
} else if (this.size === 2) {
const value = this.head.value;
this.head = this.tail;
this.tail.prev = this.tail;
this.tail.next = this.tail;
this.size--;
return value;
} else if (this.size === 1) {
const value = this.head.value;
this.head = null;
this.tail = null;
this.size--;
return value;
} else {
return -1;
}
}
empty() {
return this.size ? 0 : 1;
}
front() {
return this.head ? this.head.value : -1;
}
back() {
return this.tail ? this.tail.value : -1;
}
}
const myQueue = new Queue();
const output = [];
for (let i=1; i<input.length; i++) {
const [command, value] = input[i].split(" ");
switch(command) {
case "push": myQueue.push(value); break;
case "pop": output.push(myQueue.pop()); break;
case "size": output.push(myQueue.size); break;
case "empty": output.push(myQueue.empty()); break;
case "front": output.push(myQueue.front()); break;
case "back": output.push(myQueue.back()); break;
}
}
console.log(output.join("\n"));
Queue 클래스를 구현하여 해결하였다.
'연습장 > 백준(BOJ) 문제풀이' 카테고리의 다른 글
[백준 1120] 문자열 with Node.js (0) | 2021.04.26 |
---|---|
[백준 9461] 파도반 수열 with Python (0) | 2021.04.26 |
[백준 11727] 2×n 타일링 2 with Python (0) | 2021.04.25 |
[백준 11656] 접미사 배열 with Node.js (0) | 2021.04.22 |
[백준 2193] 이친수 with Python (0) | 2021.04.22 |