题解 | 【模板】栈

【模板】栈

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);
        }
    } 
});

全部评论

相关推荐

开发转测第二人:没实习的话,两个项目吧,八股也要准备一下,这个时间点有点小晚了
点赞 评论 收藏
分享
02-23 19:06
已编辑
西北工业大学 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务