문제
제한 사항
입출력 예
풀이
class Queue {
constructor() {
this.arr = [];
}
size() {
return this.arr.length;
}
enqueue(item) {
this.arr.push(item);
}
dequeue() {
if (!this.arr.length) {
return -1;
} else {
const result = this.arr.shift();
return result;
}
}
front() {
if (!this.arr.length) {
return -1;
} else {
return this.arr[0];
}
}
back() {
if (!this.arr.length) {
return -1;
} else {
return this.arr[this.arr.length - 1];
}
}
isEmpty() {
if (!this.arr.length) {
return 1;
} else {
return 0;
}
}
}
const input = require('fs').readFileSync('/dev/stdin').toString().trim().split('\n');
const N = +input.shift();
const myQueue = new Queue();
let answer = "";
input.forEach((e, i) => {
const [cmd, item] = e.split(" ");
if (cmd === "push") {
myQueue.enqueue(item);
} else if (cmd === "front") {
answer += myQueue.front() + "\n";
} else if (cmd === "size") {
answer += myQueue.size() + "\n";
} else if (cmd === "pop") {
answer += myQueue.dequeue() + "\n";
} else if (cmd === "back") {
answer += myQueue.back() + "\n";
} else {
if (myQueue.isEmpty()) {
answer += 1 + "\n";
} else {
answer += 0 + "\n";
}
}
});
console.log(answer.trim());