 # Create a Circular Queue

## Problem Explanation

• In this challenge we create a Circular Queue data structure.

• First, we need to create an array of `size` with all elements set to `null`.

• Then we create an equeue method, which moves the write pointer but doesnt exceed the read pointer.

• The dequeue method on the other hand, moves the read pointer but doesnt exceed the write pointer.

• Example:

• First, we create an array of length 5:
``````  [null, null, null, null, null]
^Write @ 0
``````
• Then we enqueue `a`, `b`, and `c`:
``````  [a, b, c, null, null]
^Write @ 3
``````
• Now we dequeue all the enqueued items:
``````  [null, null, null, null, null]
^Write @ 3
``````
• Finally, we enqueue `d`, `e` and `f`:
``````  [f, null, null, d, e]
^Write @ 1
``````

## Solutions

Solution 1 (Click to Show/Hide)
``````class CircularQueue {
constructor(size) {
this.queue = [];
this.write = 0;
this.max = size - 1;

while (size > 0) {
this.queue.push(null);
size--;
}
}

print() {
return this.queue;
}

enqueue(item) {
if (this.queue[this.write] === null) {
this.queue[this.write++] = item;

if (this.write > this.max) this.write = 0;
return item;
}
return null;
}

dequeue() {