CLRS gives the following implementation for a queue's enqueue and dequeue operations
head = 1 tail = 1 ENQUEUE(Q, x) Q[Q.tail] = x if Q.tail == Q.length Q.tail = 1 else Q.tail = Q.tail + 1 DEQUEUE(Q) x = Q[Q.head] if Q.head == Q.length Q.head = 1 else Q.head = Q.head + 1 return x
but I'm having trouble understanding why both
if Q.tail == Q.length Q.tail = 1
if Q.head == Q.length Q.head = 1
are needed. What would be a conceptual (or possibly visual) explanation of these two if-statements?