[1, 2, 3, 4, 5]Here the read and write are both at position
^Read @ 0
^Write @ 0
0
. Now the queue gets 3 new records a
, b
, and c
. Our queue now looks like:
[a, b, c, 4, 5]As the read head reads, it can remove values or keep them:
^Read @ 0
^Write @ 3
[null, null, null, 4, 5]Once the write reaches the end of the array it loops back to the beginning:
^Read @ 3
^Write @ 3
[f, null, null, d, e]This approach requires a constant amount of memory but allows files of a much larger size to be processed. Instructions: In this challenge we will implement a circular queue. The circular queue should provide
^Read @ 3
^Write @ 1
enqueue
and dequeue
methods which allow you to read from and write to the queue. The class itself should also accept an integer which you can use to specify the size of the queue when you create it. We've written the starting version of this class for you in the code editor. When you enqueue items to the queue, the write pointer should advance forward and loop back to the beginning once it reaches the end of the queue. Likewise, the read pointer should advance forward as you dequeue items. The write pointer should not be allowed to move past the read pointer (our class won't let you overwrite data you haven't read yet) and the read pointer should not be able to advance past data you have written.
In addition, the enqueue
method should return the item you enqueued if it is successfully and otherwise return null
. Similarly, when you dequeue an item it should be returned and if you cannot dequeue you should return null
.
enqueue
method adds items to the circular queue.
testString: assert((function(){ var test = new CircularQueue(3); test.enqueue(17); test.enqueue(32); test.enqueue(591); var print = test.print(); return print[0] === 17 && print[1] === 32 && print[2] === 591; })(), 'The enqueue
method adds items to the circular queue.');
- text: You cannot enqueue items past the read pointer.
testString: assert((function(){ var test = new CircularQueue(3); test.enqueue(17); test.enqueue(32); test.enqueue(591); test.enqueue(13); test.enqueue(25); test.enqueue(59); var print = test.print(); return print[0] === 17 && print[1] === 32 && print[2] === 591; })(), 'You cannot enqueue items past the read pointer.');
- text: The dequeue
method dequeues items from the queue.
testString: assert((function(){ var test = new CircularQueue(3); test.enqueue(17); test.enqueue(32); test.enqueue(591); return test.dequeue() === 17 && test.dequeue() === 32 && test.dequeue() === 591; })(), 'The dequeue
method dequeues items from the queue.');
- text: After an item is dequeued its position in the queue should be reset to null
.
testString: assert((function(){ var test = new CircularQueue(3); test.enqueue(17); test.enqueue(32); test.enqueue(672); test.dequeue(); test.dequeue(); var print = test.print(); return print[0] === null && print[1] === null && print[2] === 672; })(), 'After an item is dequeued its position in the queue should be reset to null
.');
- text: Trying to dequeue past the write pointer returns null
and does not advance the write pointer.
testString: assert((function(){ var test = new CircularQueue(3); test.enqueue(17); test.enqueue(32); test.enqueue(591); return test.dequeue() === 17 && test.dequeue() === 32 && test.dequeue() === 591 && test.dequeue() === null && test.dequeue() === null && test.dequeue() === null && test.dequeue() === null && test.enqueue(100) === 100 && test.dequeue() === 100; })(), 'Trying to dequeue past the write pointer returns null
and does not advance the write pointer.');
```