Tell us what’s happening:
Hey all,
Struggling with this one. I’m failing the last test: " Trying to dequeue past the write pointer returns null
and does not advance the write pointer." Any help would be appreciated.
Your code so far
class CircularQueue {
constructor(size) {
this.queue = [];
this.read = 0;
this.write = 0;
this.max = size - 1;
while (size > 0) {
this.queue.push(null);
size--;
}
}
print() {
return this.queue;
}
isFull(){
let count = 0;
for (let i = 0; i < this.queue.length; i++){
if (this.queue[i] == null){
count++
}
}
if (count > 0){
return false;
}
return true;
}
enqueue(item) {
// Only change code below this line
if (this.isFull()) {
this.write = 0;
}
if (this.queue[this.write] === null){
this.queue[this.write] = item;
this.write++;
return this.queue[this.write - 1];
} else {
return null;
}
// Only change code above this line
}
dequeue() {
// Only change code below this line
if (this.read > this.max){
this.read = 0;
}
if (this.read <= this.write){
var result = this.queue.splice(this.read, 1, null);
if (this.read < this.write){
this.read++;
}
return result[0];
} else {
return null;
}
}
// Only change code above this line
}
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:62.0) Gecko/20100101 Firefox/62.0
.
Link to the challenge:
https://learn.freecodecamp.org/coding-interview-prep/data-structures/create-a-circular-queue