Collection Kit

Queue

Linear

A Queue is a linear data structure that follows the First In First Out (FIFO) principle. Elements are added at the back and removed from the front.

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

Key Features

  • FIFO Order: The first element added is the first one to be removed.
  • Dynamic Size: Can grow and shrink as elements are added or removed.

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 queue.

Example Code

import { Queue } from "collection-kit";

const queue = new Queue();
queue.enqueue(10);
queue.enqueue(20);
queue.enqueue(30);

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