trekhleb / javascript-algorithms

📝 Algorithms and data structures implemented in JavaScript with explanations and links to further readings
MIT License
184.96k stars 29.83k forks source link

Add Circular Queue Implementation #1137

Closed Hasti522004 closed 1 month ago

Hasti522004 commented 1 month ago

Pull Request: Add Circular Queue Implementation

Description

This pull request introduces a new implementation of a Circular Queue in JavaScript. The Circular Queue is a linear data structure that follows the First In First Out (FIFO) principle and efficiently utilizes memory by wrapping around when the end of the array is reached.

Changes Made

Motivation

The motivation behind this addition is to provide an efficient queue implementation that can handle wrap-around cases seamlessly. This is particularly useful for scenarios where memory utilization is crucial and can help in reducing memory overhead compared to a standard queue implementation.

Example Usage

import CircularQueue from './CircularQueue';

const cq = new CircularQueue(5);

cq.enqueue(10);
cq.enqueue(20);
cq.enqueue(30);
cq.enqueue(40);
cq.enqueue(50);

console.log(cq.toString()); // Output: "10 20 30 40 50"

cq.dequeue();
console.log(cq.peek()); // Output: 20

cq.enqueue(60);
console.log(cq.toString()); // Output: "20 30 40 50 60"

Additional Context