Circular Queue Operations

Queue Circular Queue Data Structures

Circular Queue Operations

On December 12, many communities celebrate the Feast of Our Lady of Guadalupe with gatherings and organized queues. In this problem, you will implement a circular queue with a fixed capacity to support common queue operations.

Problem Description

Implement a circular queue that adheres to the First In, First Out (FIFO) principle. Your circular queue should include the following operations:

  • enQueue(value): Insert an element into the circular queue. Return true if the operation is successful; otherwise, return false.
  • deQueue(): Remove an element from the circular queue. Return true if the operation is successful; otherwise, return false.
  • Front(): Retrieve the front item from the queue. If the queue is empty, return -1.
  • Rear(): Retrieve the last item from the queue. If the queue is empty, return -1.
  • isEmpty(): Check whether the queue is empty. Return true if it is; otherwise, return false.
  • isFull(): Check whether the queue is full. Return true if it is; otherwise, return false.

Since the queue is circular, when the end of the queue is reached, it should wrap around to the beginning if there is available space.

Example

MyCircularQueue circularQueue = new MyCircularQueue(3); // queue capacity is 3
circularQueue.enQueue(1);  // returns true
circularQueue.enQueue(2);  // returns true
circularQueue.enQueue(3);  // returns true
circularQueue.enQueue(4);  // returns false since the queue is full
circularQueue.Rear();      // returns 3
circularQueue.isFull();    // returns true
circularQueue.deQueue();   // returns true
circularQueue.enQueue(4);  // returns true
circularQueue.Rear();      // returns 4

Starter Code

Below is some language-agnostic starter code outlining the class and its methods. Complete the implementation for your chosen programming language.

// MyCircularQueue class:
//   Constructor: MyCircularQueue(k: number) // initializes the queue with a max size k
//   enQueue(value: number): boolean
//   deQueue(): boolean
//   Front(): number
//   Rear(): number
//   isEmpty(): boolean
//   isFull(): boolean