Implement a queue of int on a circular array of fixed capacity, with fields data, front (index of the oldest element, initially 0) and size. The array never grows.
enqueuestores at(front + size) % capacity;dequeuereturnsdata[front]and setsfront = (front + 1) % capacity.
Input: the capacity (≥ 1) on the first line, then one command per line until the input ends:
| Command | Output |
|---|---|
enqueue x |
nothing, or full if the queue already holds capacity elements (x is then discarded) |
dequeue |
the removed value, or empty |
peek |
the front value without removing it, or empty |
size |
size <n> |
print |
front=<front index> followed by the elements from front to back, e.g. front=2 [30, 40, 50] |
Example with capacity 3:
enqueue 1
enqueue 2
enqueue 3
enqueue 4
print
dequeue
enqueue 4
print
prints
full
front=0 [1, 2, 3]
1
front=1 [2, 3, 4]
(4 was stored in slot 0: the queue wrapped round.)