题解 | 【模板】栈
【模板】栈
https://www.nowcoder.com/practice/104ce248c2f04cfb986b92d0548cccbf?tpId=308&tqId=2111163&sourceUrl=%2Fexam%2Foj%3FquestionJobId%3D10%26subTabName%3Donline_coding_page
class Stack {
constructor(item) {
this.item = item;
this.arr = [];
}
push(item) {
this.arr.push(item);
}
pop() {
return this.arr.length === 0 ? 'error' : this.arr.pop();
}
top() {
return this.arr.length === 0 ? 'error' : this.arr[this.arr.length-1];
}
}
const readline = require('readline');
const stack = new Stack();
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.on('line', function (line) {
const ops = line.split(' ');
const command = ops[0];
const arg = ops[1];
if (typeof stack[command] === 'function') {
const res = stack[command](arg);
if (res !== null && res !== undefined) {
console.log(res);
}
}
});
查看9道真题和解析