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