1class CircularQueue {
2 constructor(size) {
3 this.queue = [];
4 this.read = 0;
5 this.write = 0;
6 this.max = size - 1;
7
8 while (size > 0) {
9 this.queue.push(null);
10 size--;
11 }
12 }
13
14 print() {
15 return this.queue;
16 }
17
18 enqueue(item) {
19 if (this.queue[this.write] === null) {
20 this.queue[this.write++] = item;
21
22 if (this.write > this.max) this.write = 0;
23 return item;
24 }
25 return null;
26 }
27
28 dequeue() {
29 if (this.queue[this.read] != null) {
30 let item = this.queue[this.read];
31 this.queue[this.read++] = null;
32 if (this.read > this.max) this.read = 0;
33 return item;
34 }
35 return null;
36 }
37}
38