class Queue {
constructor() {
this.storage = {};
this.front = 0; //앞
this.rear = 0; //뒤
}
size() {
return this.rear - this.front;
}
// 큐에 데이터를 추가 할 수 있어야 합니다.
enqueue(element) {
this.storage[this.rear] = element;
this.rear += 1;
}
// 가장 먼저 추가된 데이터가 가장 먼저 추출되어야 합니다.
dequeue() {
// 빈 큐에 dequeue 연산을 적용해도 에러가 발생하지 않아야 합니다
if (this.rear === this.front) {
return;
}
const result = this.storage[this.front];
delete this.storage[this.front];
this.front += 1;
return result;
}
}
queue는 현재
{ storage,
front = 0,
rear = 0 } 이렇게 생겼다.
빈 큐에 1을 넣어보자.
this.storage[this.rear]은 현재 rear값이 키가 된다는 뜻이다.
rear는 현재 0이다. 키는 0이고, 값은 1이 될 것이다.
그리고 rear에 +1해준다.