원형 형태를 가지며, 먼저 넣은 데이터가 먼저 나오는 FIFO(First In First Out) 기반의 선형 자료 구호
원형연결리스트처럼 처음과 끝이 이어져 있는 구조

기존 큐와 달리 데이터가 꽉 찼는지 확인하는 메서드가 존재한다. (CircularQueue.isFull())
function CircularQueue(array = [], size = DEFAULT_SIZE) {
this.array = array;
this.size = array.length > size ? array.length : size; // CirularQueue의 사이즈
this.length = array.length; // array의 size
this.head = 0;
this.tail = array.length;
}
CircularQueue.prototype.getBuffer = function() {
return this.array.slice();
}
CircularQueue.prototype.isEmpty = function() {
return this.length === 0;
}
CircularQueue.prototype.isFull = function() {
return this.size === this.length;
}
CircularQueue.prototype.enqueue = function(element) {
if (this.isFull()) return false;
this.array[this.tail] = element;
this.tail = (this.tail + 1) % this.size;
this.length++;
return true;
}
데이터 추가와 비슷하게 head를 기준으로 삭제한다.
CircularQueue.prototype.dequeue = function() {
if (this.isEmpty()) return undefined;
let element = this.array[this.head];
delete this.array[this.head];
this.head = (this.head + 1) % this.size;
this.length--;
return element;
}
array의 head값을 반환한다.
CircularQueue.prototype.front = function() {
return this.length === 0 ? undefined : **this.array[this.head]**;
}
CircularQueue.prototype.dataSize = function() {
return this.length;
}
원형큐의 크기는 그대로 유지한 후 초기화 시킨다.
CircularQueue.prototype.clear = function(size = DEFAULT_SIZE) {
this.array = [];
this.size = size;
this.length = 0;
this.head = 0;
this.tail = 0;
};
관련 전체 코드는 Git에 업로드 해두었습니다.
Github_CircularQueue