aashish578

Aashish Panchal

Posted on October 16, 2020

Queue
class Node {

    constructor(value) {

        this.value = value;

        this.next = null;

    }

}

class Queue {

    constructor() {

        this.first = null;

        this.last = null;

        this.size = 0;

    }

    // Add Value in the tail

    enqueue(val) {

        var newNode = new Node(val);

        if (!this.first) {

            this.first = newNode;

            this.last = newNode;

        } else {

            this.last.next = newNode;

            this.last = newNode;

        }

        console.log(`--> You are ${++this.size} Inserted Value and this is a <- ${val}`);

    }

    // Delete Value in head

    dequeue() {

        if (!this.first) return null

        var temp = this.first;

        if (this.first === this.last) {

            this.last = null;

        }

        this.first = this.first.next;
        this.size--;
        console.log(`Delete Successfully 👍👍 `)
        return temp.value;

    }

    // Show Head Value

    peek() {

        return this.first

    }

}

var q = new Queue()
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
aashish578
Aashish Panchal

Posted on October 16, 2020

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

Sign up to receive the latest update from our blog.

Related