Writing a queue in JS (using an Array)

Pere Sola - Sep 18 '20 - - Dev Community

I recently started to listen to the Base CS podcast and it brings me fond memories from my 2 months of Computer Science at Lambda School - I loved learning about and building data structures. While trying to solve a Kata today, I saw an opportunity to use Queue. I had to refresh the JS Class syntax since I had forgotten - I don't use it every day. I thought I would write about Queues here because, well, to remember about it since this is what this series is about!

A Queue is FIFO (First In First Out) - the one that got first in the queue is the one getting out first. In my implementation, the left hand side of the queue will be the first.

At first it was going to be a simple queue but the folks at GeeksforGeeks got me inspired to add some extra methods.

As usual, if something doesn't make sense or can be improved let me know in the comments.

class Queue {
    constructor() {
        // initialise array as data structure for the queue
        this.queue = [];
    }

    add(item) {
        // we add an item at the end of the array
        this.queue.push(item);
        console.log(
            `${item} has been added to the queue. The queue is now ${this.queue}`
        );
        return this.queue;
    }

    isEmpty() {
        // if queue length is 0, return true otherwise return false
        return this.queue.length === 0;
    }

    remove() {
        // if queue is empty, we print error message
        if (this.isEmpty()) {
            console.log("Can't remove anything, the queue is empty");
            return this.queue;
        }
        // we remove first item of the array
        // from my teacher at Lambda I learnt that the method is called
        // "shift" because when you remove first item of array in memory
        // we need to "shift" the whole array one space to the left in memory
        // it would then be O(n), am I horrinly wrong??
        const removed = this.queue.shift();
        console.log(
            `${removed} has been removed from the q. q is now ${this.queue}`
        );
        return this.queue;
    }

    print() {
        // print error if queue is empty
        if (this.isEmpty()) {
            console.log("Can't print anything, the queue is empty");
            return this.queue;
        }
        // initialise string
        let queue = '';
        // according to mdn I am not supposed to use for..in for arrays, but it works
        // well to get the index. Is it that bad??
        for (let i in this.queue) {
            // we add the item of the queuea and ", ". if last item, we don't add nothing behind
            queue +=
                this.queue[i] + `${parseInt(i) === this.queue.length - 1 ? '' : ', '}`;
        }
        return queue;
    }

    size() {
        // returns length of the array this.queue
        console.log(`The queue has ${this.queue.length} items.`);
        return this.queue.length;
    }

    returnFirst() {
        if (this.isEmpty()) {
            console.log('Queue is empty');
            return this.queue;
        }
        // return first item in the queue
        return this.queue[0];
    }
}
Enter fullscreen mode Exit fullscreen mode
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .