Let's say I implement a circular queue using an array. How could I calculate the size of the queue? By size I mean the number of elements between the front and the rear. I want to use the modulo operation.
I have the capacity of the array, and the positions of the front and the rear of the queue. I don't known what to do now.
How could I calculate the size of the queue?
I would use
size = (start - end + mod) % mod;
This assumes the buffer is never completely at capacity. An alernative is to use a start and end which is without modding
size = lastWriteIndex - nextReadIndex;
You can mod these value when you look up the index.