DEV Community

loading...
Cover image for Sumo-Queue: Queue Data Structure Implementation in JavaScript

Sumo-Queue: Queue Data Structure Implementation in JavaScript

boxpiperapp profile image BOX PIPER ・2 min read

As per Wikipedia, In computer science, a queue is a collection of entities that are maintained in a sequence and can be modified by the addition of entities at one end of the sequence and the removal of entities from the other end of the sequence.

The Queue is a data structure following the FIFO(First In, First Out) principle. When you are dealing with larger arrays, it's always suggested to use Queue, because of constant time complexity O(1).

The process of adding in a queue is known as Enqueue and removal is known as Dequeue. Front and Back signifies element at those respective positions. The queue capacity is known at the time of initialisation and it's the max number of elements a queue can hold.

queue-and-its-basic-operations-operations-9bfc0ef1b53c11f9.png

With all the available information about queue and knowledge of JavaScript, pushed me to write a tiny package which implements this with almost simplicity.

Thus I wrote a code, named it SUMO QUEUE, pushed it to GitHub and published to NPM. Do check it out.

github-npm.jpg

GitHub: https://github.com/sanmak/sumo-queue/

NPM: https://www.npmjs.com/package/sumo-queue

SUMO QUEUE

Sumo Queue has all the queue operations implemented. Use it especially for the larger array to attain O(1) complexity.

This package is built with raw javascript and Mocha / Chai is used for testing.

Installation and Usage

npm i --save sumo-queue
Enter fullscreen mode Exit fullscreen mode
# Import Package
const Queue = require("sumo-queue");
Enter fullscreen mode Exit fullscreen mode
# Initialisation
const queue = new Queue(2);
// OUTPUT: 1609158613385wbQvkB5djUXB debug log: A new queue 1609158613385wbQvkB5djUXB is initialized with capacity 2

Enter fullscreen mode Exit fullscreen mode
# Enqueue operation
queue.enqueue(1);
// OUTPUT: 1609158613385wbQvkB5djUXB debug log: Node added {"currentPointerValue":1,"nextPointer":null}

Enter fullscreen mode Exit fullscreen mode
# Iteration
console.log(queue.iterate());
// OUTPUT: [ 1 ]
Enter fullscreen mode Exit fullscreen mode
# To get first element value
console.log(queue.first().currentPointerValue);
// OUTPUT: 1
Enter fullscreen mode Exit fullscreen mode

There is a lot of features offered by this package. Check the readme for more information. https://github.com/sanmak/sumo-queue/blob/main/README.md

Open Source Contribution

I have mentioned that it's open for contribution and feel free to add new features. Contributions are what makes the open-source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

License

This package is distributed under MIT license.

Contact

SANKET MAKHIJA - @sanket_dude - sanket[dot]mahija[at]gmail[dot]com

Sponsor ❤️

Consider sponsoring this package and help open source community and contributions.

To read interesting topics, follow and read BoxPiper blog.

Tech, Product, Money, Books, Life. Box Piper to serve you faster, easier and superior knowledge.

Discussion (0)

pic
Editor guide