class QNode {
    constructor(value) {
        this.value = value;
        this.next = null;
    }
}

class Queue {
    #front = null;
    #reer = null;
    constructor() {
        this.size = 0;
    }
    push (x) {
        const newNode = new QNode(x);
        if (!this.size) {
            this.#front = newNode;
            this.#reer = newNode;
        } else {
            this.#reer.next = newNode;
            this.#reer = newNode;
        }

        this.size++;
    }
    pop() {
        if (this.size !== 0) {
            const nextNode = this.#front.next;
            const { value } = { ...this.#front };
            this.#front = nextNode;
            this.size--;
            return value;
        }
    }
    top() {
        if (this.size !== 0) {
            return this.#front.value;
        }
    }
}

'algorithm > algorithm' 카테고리의 다른 글

STL 기억안나는 부분 정리  (0) 2021.01.23
Binary , lower , upper searching  (0) 2020.09.24
이분 매칭(Bipartite Matching)  (0) 2020.06.22
네트워크 플로우(Maximum flow)  (0) 2020.06.22
위상 정렬(Topological Sort)  (0) 2020.06.22

+ Recent posts