Collection Kit

CircularQueue

Linear

A CircularQueue is a linear data structure that uses a fixed-size array in a circular manner, allowing for efficient use of space.

Import Statement
import { CircularQueue } from "collection-kit";

Key Features

  • Circular Structure: The last position is connected back to the first position, allowing for efficient space utilization.
  • Fixed Size: Has a maximum capacity, which can lead to overflow if not managed properly.

Common Operations

Enqueue
Add an element to the back of the queue.
Dequeue
Remove and return the front element.
Peek
Return the front element without removing it.
Size
Return the number of elements in the circular queue.

Example Code

import { CircularQueue } from "collection-kit";

const queue = new CircularQueue(5); // capacity of 5
queue.enqueue(10);
queue.enqueue(20);
queue.enqueue(30);

console.log("Front:", queue.peek());      // 10
console.log("Dequeued:", queue.dequeue()); // 10
console.log("Size:", queue.size());       // 2