Queue(큐)
· 약 2분
1. Array로 큐를 구현
- 단점
- DeQueue를 하면 배열 앞부분이 공백이 된다.
- 배열에서 리얼 인덱스 값이 무한정 커질 수 있다.
javascript
class Queue {
constructor() {
this.queue = [];
this.head = 0;
this.tail = 0;
}
// queue 대기행렬에 데이터 입력
// tail 쪽에 데이터가 추가됨
enqueue(value) {
this.queue[this.tail++] = value;
}
// queue 대기행렬에서 데이터 출력
// head 쪽 데이터가 꺼내지고 데이터들이 앞으로 옮겨짐
dequeue() {
const value = this.queue[this.head];
delete this.queue[this.head];
this.head += 1;
return value;
}
peek() {
return this.queue[this.head];
}
size() {
return this.tail - this.head;
}
}