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.
- 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.
To install the package globally, use the following command:
npm install -g namastey-circular-queue
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()); // Output: 1
queue.printQueue(); // Output: 1 2 3 4 5
queue.dequeue();
console.log(queue.peek()); // Output: 2
queue.enqueue(6);
queue.printQueue(); // Output: 6 2 3 4 5