이카's
Published 2021. 6. 2. 09:56
[JavaScript] 스택 구현 BOJ

class Stack {
    constructor () {
        this.stack = [];
        this.size = 0;
    }

    push(arg) {
        this.size++;
        this.stack = this.stack.concat(arg);
    }

    size() {
        return this.size;
    }

    pop() {
        if (this.stack.length === 0) {
            return -1
        }
        const popNum = this.stack[this.size - 1];
        this.stack = this.stack.slice(0, -1);
        this.size--
        return popNum;
    }

    empty() {
        if (this.size === 0) {
            return 1;
        } 
        else {
            return 0;
        }
    }

    top() {
        if (this.size === 0) {
            return -1;
        } 
        else {
            return this.stack[this.size - 1];
        }
    }
};

백준에서 nodejs는 입출력 구현 하는게 더 어려운거 같다.

반응형
profile

이카's

@Edan Cafe ☕

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!