#K64597. Circular Buffer Operations
Circular Buffer Operations
Circular Buffer Operations
In this problem, you are required to implement a circular buffer that supports three operations: enqueue, dequeue, and print. The circular buffer has a fixed capacity ( n ). When an element is enqueued and the buffer is full, the oldest element is automatically removed to make room for the new element. The operations are defined as follows:
- enqueue x: Add the element ( x ) to the buffer. If the buffer is full, remove the oldest element before adding ( x ).
- dequeue: Remove and return the oldest element from the buffer. If the buffer is empty, output
EMPTY
. - print: Print all elements in the buffer from the oldest to the newest, separated by a single space. If the buffer is empty, output
EMPTY
.
Your program will receive multiple operations. For each dequeue
or print
operation, output the result on a new line.
inputFormat
The input is read from stdin and has the following format:
- The first line contains an integer ( n ), the capacity of the circular buffer.
- The second line contains an integer ( m ), the number of operations.
- The next ( m ) lines each contain an operation, which can be one of the following:
enqueue x
(where ( x ) is an integer)dequeue
print
outputFormat
For each dequeue
or print
operation, print the result on a new line to stdout. When the buffer is empty, print EMPTY
.## sample
3
9
enqueue 1
enqueue 2
enqueue 3
print
enqueue 4
print
dequeue
enqueue 5
print
1 2 3
2 3 4
2
3 4 5
</p>