DEV Community

Discussion on: Javascript: How to implement a queue

Collapse
 
zessu profile image
A Makenzi • Edited

Slightly confused over the need for last=(Last+1)%arr.lergth
Being that (x-1)%x always= x-1, shouldn't last just be=Last+1
Cheers

Collapse
 
aminmansuri profile image
hidden_dude

Where did you see x-1?

There should be no subtraction at all in a Queue.

The first pointer increments when you remove items. The last pointer increments when you add items.

The modulus is important to make the Queue circular and reuse the unused space. But in your implementation you should check that you aren't already full before accepting to add items.

Hope this helps.

Some comments have been hidden by the post's author - find out more