There is a circular queue in which the head pointer is front and the tail pointer is rear; the length of the circular queue is N. What is the effective length of the team?

There is a circular queue in which the head pointer is front and the tail pointer is rear; the length of the circular queue is N. What is the effective length of the team? (Assuming that the head of the
queue does not store data) The length of the circular queue space is fixed to N.
For a simple example, the space position is 1, 2, 3, 4, 5, 6, and the space length is 6
There is no data in the front of the body.
If front < = rear, then (rear-front)> 0, the actual space length is (rear-front), for example, front = 1, rear = 4,
if front> rear, then (rear-front) <0, the actual length is (rear + N-front), for example, front = 5, rear = 2
In order to unify the two cases, the result given is (rear-front + N)% N

If data is also stored in front,
the result is
(rear-front+1+N)% N

Guess you like

Origin blog.csdn.net/qq_43360777/article/details/106988302