#C13883. Circular Queue Operations
Circular Queue Operations
Circular Queue Operations
Implement a Circular Queue data structure with a fixed capacity \(k\) that supports the following operations:
- enqueue x - Insert an integer \(x\) at the rear of the queue. Returns \(\text{true}\) if the operation is successful or \(\text{false}\) if the queue is full.
- dequeue - Remove an element from the front of the queue. Returns \(\text{true}\) if the operation is successful or \(\text{false}\) if the queue is empty.
- Front - Return the front element of the queue. Returns \(-1\) if the queue is empty.
- Rear - Return the last element of the queue. Returns \(-1\) if the queue is empty.
- isEmpty - Check if the queue is empty. Returns \(\text{true}\) or \(\text{false}\).
- isFull - Check if the queue is full. Returns \(\text{true}\) or \(\text{false}\).
Your program should read commands from standard input and output the result of each operation on a new line. Make sure that each output strictly corresponds to the result of the executed command in the order given.
inputFormat
The first line contains two integers \(k\) and \(n\), where \(k\) is the maximum capacity of the circular queue and \(n\) is the number of operations.
Each of the following \(n\) lines contains one command in one of the following formats:
enqueue x
— where \(x\) is an integer.dequeue
Front
Rear
isEmpty
isFull
outputFormat
For each operation, output its result on a separate line:
- For
enqueue
anddequeue
operations, outputtrue
orfalse
(in lowercase) indicating success or failure. - For
Front
andRear
operations, output the integer at the front or rear. If the queue is empty, output-1
. - For
isEmpty
andisFull
operations, outputtrue
orfalse
(in lowercase).
3 18
enqueue 1
enqueue 2
enqueue 3
enqueue 4
Front
Rear
isFull
dequeue
enqueue 4
Front
Rear
dequeue
dequeue
dequeue
dequeue
isEmpty
Front
Rear
true
true
true
false
1
3
true
true
true
2
4
true
true
true
false
true
-1
-1
</p>