DEV Community

Bvnkumar
Bvnkumar

Posted on • Updated on

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.

Top comments (0)