Create a Circular Queue

Create a Circular Queue
0.0 0

#1

Tell us what’s happening:
“Trying to dequeue past the write pointer returns null and does not advance the write pointer.” this is the only task that’s giving me a headache. the checkEmpty() method does that, if the queue is emtpy, nothing happens and the the write pointer stays put. what am I doing wrong? thx in advance

Your code so far


class CircularQueue {
   constructor(size) {
    
     this.queue = [];
     this.read = 0;
     this.write = 0;
     this.max = size - 1;
     this.count=0;

     while (size > 0) {
        this.queue.push(null);
        size--;
     }
   }
  checkEmpty(){
   var count=0;
      for(let i=0;i<=this.max;i++){
        if(this.queue[i]==null){
          count++;
        }
      }
    if(count==this.max+1){
      return true;
    }
    return false;
  }
  checkFull(){
      var count=0;
      for(let i=0;i<=this.max;i++){
        if(this.queue[i]==null){
          count++;
        }
      }
     if(count>0) {
     return false;
    }
    return true;
   }
  
   print() {
     return this.queue;
   }

   enqueue(item) {
    // Only change code below this line
    if(!this.checkFull()){
       this.queue[this.write]=item;
       this.write=(this.write+1)%(this.max+1);
       }
      } 
    // Only change code above this line
  
   dequeue() {
    // Only change code below this line
     if(!this.checkEmpty()){
       let val=this.queue[this.read];
       this.queue[this.read]=null;
       this.read=(this.read+1)%(this.max+1);
       return val;
     }
    else return null;
    // Only change code above this line
   }
}

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/coding-interview-prep/data-structures/create-a-circular-queue


#2

I would not use the checkEmpty method at all. For reading you only have to worry about the current record. Checking that the whole queue is empty doesn’t seem correct. That is, I think you woukd check if there is something to read at the current point. If yes, return it, if not, return null. Ignore all other points in the queue.


#3

replaced “if(!this.checkEmpty())” with “if(this.queue[this.read])” and nothing changed :frowning:


#4

dequeue() {
// Only change code below this line
let val=this.queue[this.read];
if (val) {
// this.queue[this.read]=null; //don’t need this, as this function is only for reading
this.read=(this.read+1)%(this.max+1); //change the read counter
return val;
}
else return null;
// Only change code above this line
}

So if the above is your new method, then looking at it, I would say that you still need to worry about the write counter’s position. If the write counter is, for example at position 5 and you are trying to read position 5 or greater, then you should return null.

Let’s think about it in more concrete examples:

Someone writes (enqueues) 5 records A, B, C, D, E
And max size of queue is 6 so my write counter is pointing to the sixth position
I then try to read at position 1, 2, 3, 4, 5 and then try to read 6, then I should get a null.
Another eg. what if someone enqueues 6 records A, B, C, D, E, F then tries to read 7?
So then that would mean that my write counter after writing 6 is pointing at the A again, while my read counter after 6 read is pointing at the A (ready to read A). Your code should say ‘stop right there’ because ‘A’ is also where the write counter is pointing, therefore, I should not re-read A.

At least, this is how I interpret the success criteria “Trying to dequeue past the write pointer…” , so I hope this helps.


#5

so, from what you’re saying, I cannot have at some point an empty queue like queue.length=4 =[null, null, null, null]. Because that means I understood the problem wrong. ok, will try to adress this problem and let you know


#6

Not quite, but I was trying to say that you should check the position of the write versus the read pointers. At no point should you be trying to read something that you haven’t written yet (and that does not depend on null, rather it just depends on current position of the pointers). You can read A, B, C, D, E, F only once. You should not re-read A, B etc just because someone calls enqueue. Only read them if the write counter is ahead of the read.