Queue란❓

큐는 FIFO 원칙하에 운용되는 자료구조이다. 즉 가장 먼저 들어온 데이터가 가장 먼저 빠져나가야 하는 구조이며, 이는 스택과 상반되는 순서를 가진다.

Untitled

🤔 굳이 구현해야 할까?

💡 구현

export default class MyQueue {
    constructor() {
        this.input = [];
        this.output = [];
        this.size = 0;
    }
    enque(val) {
        this.input.push(val);
        this.size++;
    }
    deque() {
        if (this.output.length === 0) {
            this.output = this.input.reverse();
            this.input = [];
        }
        this.size--;
        return this.output.pop();
    }
}

🍒 구현 포인트

FIFO를 위한 작업