Question

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

and

if Q.head == Q.length
    Q.head = 1

are needed. What would be a conceptual (or possibly visual) explanation of these two if-statements?

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with cs.stackexchange
scroll top