
var MyCircularQueue = function(k) {
this.k = k;
this.arr = new Array(this.k);
this.headIdx = -1;
this.tailIdx = -1;
};
MyCircularQueue.prototype.enQueue = function(value) {
if(this.isFull()){
return false;
}
if(this.isEmpty()){
this.headIdx = 0;
}
this.tailIdx = (this.tailIdx + 1) % this.k;
this.arr[this.tailIdx] = value;
return true;
};
MyCircularQueue.prototype.deQueue = function() {
if(this.isEmpty()) {
return false;
}
if(this.headIdx === this.tailIdx) {
this.headIdx = -1;
this.tailIdx = -1;
} else {
this.headIdx = (this.headIdx + 1) % this.k;
}
return true;
};
MyCircularQueue.prototype.Front = function() {
if(this.isEmpty()) {
return -1;
}
return this.arr[this.headIdx];
};
MyCircularQueue.prototype.Rear = function() {
if(this.isEmpty()) {
return -1;
}
return this.arr[this.tailIdx];
};
MyCircularQueue.prototype.isEmpty = function() {
return this.headIdx === -1;
};
MyCircularQueue.prototype.isFull = function() {
return ((this.tailIdx + 1) % this.k) === this.headIdx;
};