const fs = require('fs');
// const input = fs.readFileSync('./testcase.txt').toString().trim();
const input = fs.readFileSync('/dev/stdin').toString().trim();
const inputs = input.split('\n');
const N = inputs[0];
function initMethodInArrayPrototype() {
if (
Array.prototype.at === undefined ||
typeof Array.prototype.at !== 'function'
) {
Array.prototype.at = (index) => {
if (index >= 0) {
if (index < this.length) return this[index];
else return undefined;
}
else {
const ridx = index * -1;
if (ridx > this.length) return undefined;
return this[this.length - ridx];
}
}
}
}
class Stack {
constructor() {
this.stack = [];
}
push(x) {
this.stack.push(x);
}
pop() {
if (this.stack.length === 0) {
return -1;
} else {
return this.stack.pop();
}
}
size() {
return this.stack.length;
}
empty() {
return this.stack.length ? 0 : 1;
}
top() {
if (this.stack.length) {
return this.stack.at(-1);
} else {
return -1;
}
}
}
initMethodInArrayPrototype();
const stack = new Stack();
let answer = '';
for (let i = 1; i <= N; i++) {
const [req, num] = [...inputs[i].split(' ')];
if (req === 'push') {
stack.push(+num);
}
if (req === 'pop') {
answer += stack.pop() + '\n';
}
if (req === 'size') {
answer += stack.size() + '\n';
}
if (req === 'top') {
answer += stack.top() + '\n';
}
if (req === 'empty') {
answer += stack.empty() + '\n';
}
}
console.log(answer);
https://www.acmicpc.net/problem/10828
'algorithm > BOJ' 카테고리의 다른 글
BOJ 1486번 등산 (1) | 2021.04.17 |
---|---|
BOJ 2273번 줄 서기 (0) | 2021.04.17 |
BOJ 3584번 가장 가까운 공통 조상 (0) | 2021.03.31 |
BOJ 1103번 게임 (0) | 2021.03.16 |
BOJ 2194번 유닛 이동시키기 C++ (0) | 2020.07.05 |