2
$\begingroup$

In ciruclar queue the following command statement is very useful to use the determine the unused location. What is its importance? I know that without this line it won't be a circular queue. But how to say it in theoretical way?

rear=(rear+1)%max

$\endgroup$
  • 1
    $\begingroup$ the modulus operation ensures that the new value of rear will always be greater equals 0 and smaller than max. Hence, you will neve get out of bound errors and instead continue at 0 once you have reached the end. $\endgroup$ – jboockmann Jul 7 '18 at 11:11
4
$\begingroup$

Suppose that we arrange the numbers $0,\ldots,n-1$ in an oriented cycle. The number which follows $i$ is $i+1$ for $i \neq n-1$, and $0$ for $i=n-1$. We can combine both cases by saying that the number following $i$ is $i+1 \bmod{n}$.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.