Implementing Stacks & Queues
What problem does the modulo operation solve in the circular queue enqueue method?
class CircularQueue {
constructor(size) {
this.maxSize = size;
this.items = new Array(size);
this.front = -1;
this.rear = -1;
}
enqueue(element) {
if (this.isFull()) return false;
if (this.isEmpty()) this.front = 0;
this.rear = (this.rear + 1) % this.maxSize;
this.items[this.rear] = element;
return true;
}
}