Implementing Queue in JavaScript

In this blog, we’re going to discuss how to implement queues in JavaScript. Specifically, we’ll cover two approaches: one using an array and another using a linked list.

Implementing Queue using Array in JavaScript

Let’s kick things off with a simple implementation using an array. In JavaScript, arrays are versatile data structures, making them suitable for implementing queues. Here’s a basic example:

class Queue {
  constructor() {
    this.items = [];
  }

  // Add an element to the queue
  enqueue(element) {
    this.items.push(element);
  }

  // Remove and return the front element from the queue
  dequeue() {
    if (this.isEmpty()) {
      console.log("Queue is empty");
      return null; // or throw new Error("Queue is empty");
    }
    return this.items.shift();
  }

  // Return the front element without removing it
  front() {
    if (this.isEmpty()) {
      console.log("Queue is empty");
      return null; // or throw new Error("Queue is empty");
    }
    return this.items[0];
  }

  // Check if the queue is empty
  isEmpty() {
    return this.items.length === 0;
  }

  // Return the size of the queue
  size() {
    return this.items.length;
  }

  // Print the elements of the queue
  print() {
    console.log(this.items.join(" "));
  }
}

// Example usage:
const queue = new Queue();

queue.enqueue(10);
queue.enqueue(20);
queue.enqueue(30);

console.log("Front element:", queue.front()); // Front element: 10

console.log("Queue size:", queue.size()); // Queue size: 3

console.log("Dequeue:", queue.dequeue()); // Dequeue: 10

queue.print(); // 20 30

In this implementation, we have a Queue class with methods for enqueue, dequeue, checking if the queue is empty, getting the front element, and more.

Implementing Queue using Linked List in JavaScript

Now, let’s explore another way to implement a queue in JavaScript, this time using a linked list. We’ll create a Node class to represent elements in the queue and a Queue class to manage the operations:

class Node {
  constructor(data) {
    this.data = data;
    this.next = null;
  }
}

class Queue {
  constructor() {
    this.front = null;
    this.rear = null;
    this.size = 0;
  }

  // Add an element to the queue
  enqueue(element) {
    const newNode = new Node(element);

    if (this.isEmpty()) {
      this.front = newNode;
      this.rear = newNode;
    } else {
      this.rear.next = newNode;
      this.rear = newNode;
    }

    this.size++;
  }

  // Remove and return the front element from the queue
  dequeue() {
    if (this.isEmpty()) {
      console.log("Queue is empty");
      return null; // or throw new Error("Queue is empty");
    }

    const removedNode = this.front;
    this.front = removedNode.next;

    if (this.front === null) {
      this.rear = null;
    }

    this.size--;

    return removedNode.data;
  }

  // Return the front element without removing it
  getFront() {
    if (this.isEmpty()) {
      console.log("Queue is empty");
      return null; // or throw new Error("Queue is empty");
    }
    return this.front.data;
  }

  // Check if the queue is empty
  isEmpty() {
    return this.size === 0;
  }

  // Return the size of the queue
  getSize() {
    return this.size;
  }

  // Print the elements of the queue
  print() {
    let current = this.front;
    const elements = [];

    while (current !== null) {
      elements.push(current.data);
      current = current.next;
    }

    console.log(elements.join(" "));
  }
}

// Example usage:
const queue = new Queue();

queue.enqueue(10);
queue.enqueue(20);
queue.enqueue(30);

console.log("Front element:", queue.getFront()); // Front element: 10

console.log("Queue size:", queue.getSize()); // Queue size: 3

console.log("Dequeue:", queue.dequeue()); // Dequeue: 10

queue.print(); // 20 30

In this linked list implementation, each element in the queue is represented by a Node with a data property and a next property pointing to the next node in the linked list. The Queue class manages the front and rear of the linked list and provides methods for enqueue, dequeue, and other operations on the queue.

And there you have it, two different ways to implement queues in JavaScript. Whether you prefer the simplicity of an array or the flexibility of a linked list, queues are a fundamental data structure that can be incredibly useful in various scenarios.

If you found this blog helpful, don’t forget to share on social media. Feel free to leave any questions or suggestions in the comments below. Happy coding!

Don't Miss Out! Subscribe to Read Our Latest Blogs.

If you found this blog helpful, share it on social media.

Subscription form (#5)

Leave a Comment

Your email address will not be published. Required fields are marked *

Pin It on Pinterest

Scroll to Top