DEV Community

Bvnkumar
Bvnkumar

Posted on • Edited on

3 2

Javascript Queue data structure

QUEUE:
Queue is also one type of data structure and works like a stack but it follows a first in first out(FIFO) order.
Example: When you are in a line to pay a bill at the counter, we should follow the sequence like first in first out, queue also works same.

Internal design of queue:

const Queue = function() {
    let collections = [];
    this.add = function(item) {
        return collections.push(item);
    }
    this.size = function() {
        return collections.length;
    }
    this.isEmpty = function() {
        retun(collections.length == 0)
    }
    this.enqueue = function(item) {
        return collections.push(item)
    }
    this.dequeue = function(item) {
        return collections.shift();
    }
    this.front = function() {
        return collections[0];
    }
}

let queue = new Queue();
console.log(queue.size())
queue.enqueue("1");
queue.enqueue("2");
console.log(queue.size())
console.log(queue.front());
queue.dequeue();
console.log(queue.size())
Enter fullscreen mode Exit fullscreen mode

Any comments or suggestions are welcome.

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay