题解 | #【模板】栈#
【模板】栈
https://www.nowcoder.com/practice/104ce248c2f04cfb986b92d0548cccbf
const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;
void (async function () {
//记录操作次数
const n = await readline();
//定义一个栈
const stack = [];
for (let i = 0; i < n; i++) {
//获取输入
const input = await readline();
//转换为操作数组
const action = input.split(" ");
//如果为push操作,并且有操作数,则入栈
if (action[0] === "push" && action[1]) {
stack.push(parseInt(action[1]));
} else if (action[0] === "pop") {
//pop操作
console.log(stack[stack.length - 1] ? stack.pop() : "error");
} else if (action[0] === "top") {
console.log(
stack[stack.length - 1] ? stack[stack.length - 1] : "error"
);
}
}
})();
#刷题##笔试##栈##算法##数据结构#
老板电器公司氛围 201人发布