#K50572. Circular Queue Operations

    ID: 28894 Type: Default 1000ms 256MiB

Circular Queue Operations

Circular Queue Operations

Circular Queue Operations

You are required to implement a circular queue with a fixed maximum size. The circular queue should support the following operations:

  • enqueue x: Insert the integer x into the queue. If the queue is full, the new element overwrites the oldest element.
  • dequeue: Remove the oldest element from the queue. If the queue is empty, print an error message.
  • getElements: Print the elements in the queue from the oldest to the newest. If the queue is empty, print empty.

Assume the circular queue uses 0-indexed positions and wraps around when reaching the end. In other words, if the maximum size is denoted by \( N \), then the index is computed modulo \( N \).

Implement the operations as specified and ensure your solution reads input from standard input (stdin) and outputs to standard output (stdout).

inputFormat

The first line of input contains two integers separated by a space: max_size and Q, where max_size is the maximum capacity of the circular queue and Q is the number of commands to process.

Each of the next Q lines contains a command which will be one of the following:

  • enqueue x where x is an integer.
  • dequeue
  • getElements

outputFormat

For each command, the program should behave as follows:

  • For a dequeue command, if the queue is empty, print error on a new line. Otherwise, perform the deletion.
  • For an enqueue command, simply insert the element (overwriting the oldest element if the queue is full).
  • For a getElements command, output the elements of the queue in order from oldest to newest separated by a single space. If the queue is empty, print empty.
## sample
3 5
enqueue 1
enqueue 2
enqueue 3
enqueue 4
getElements
2 3 4