namastey-circular-queue
Description
namastey-circular-queue
is a JavaScript package that implements the Circular Queue data structure with various important methods. A Circular Queue is a linear data structure that follows the FIFO (First In First Out) principle and connects the end of the queue back to the beginning, making it circular.
Features
- enqueue(item): Adds an item to the end of the queue. Throws an error if the queue is full.
- dequeue(): Removes and returns the item at the front of the queue. Throws an error if the queue is empty.
- peek(): Returns the item at the front of the queue without removing it. Throws an error if the queue is empty.
- isEmpty(): Checks if the queue is empty.
- isFull(): Checks if the queue is full.
- getSize(): Returns the current number of items in the queue.
- printQueue(): Prints the queue, showing the elements and
null
for empty slots.
Installation
To install the package globally, use the following command:
npm install -g namastey-circular-queue
Examples
const CircularQueue = require('namastey-circular-queue');
const queue = new CircularQueue(5);
queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(3);
queue.enqueue(4);
queue.enqueue(5);
console.log(queue.peek());
queue.printQueue();
queue.dequeue();
console.log(queue.peek());
queue.enqueue(6);
queue.printQueue();