Tell us what’s happening:
ohk, So I looked at the hint
for this challenge, searched in this forum, but still unable to get what am I doing wrong?
It is not passing the test: Trying to dequeue past the write pointer should return null and does not advance the write pointer.
Your code so far
class CircularQueue {
constructor(size) {
this.queue = [];
this.read = 0;
this.write = 0;
this.max = size;
while (size > 0) {
this.queue.push(null);
size--;
}
}
print() {
return this.queue;
}
enqueue(item) {
// Only change code below this line
if(this.queue[this.write]!==null) return null
this.queue[this.write++] = item;
this.write%=this.max;
// Only change code above this line
}
dequeue() {
// Only change code below this line
if(this.queue[this.read]===null) return null;
let item = this.queue[this.read];
this.queue[this.read++] = null;
this.read%=this.max;
return item;
// Only change code above this line
}
}
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36
.
Challenge: Create a Circular Queue
Link to the challenge: