Javascript Queue data structure

bvnkumar

Bvnkumar

Posted on April 18, 2022

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.

💖 💪 🙅 🚩
bvnkumar
Bvnkumar

Posted on April 18, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Javascript Queue data structure
javascript Javascript Queue data structure

April 18, 2022