📜  javascript queue (1)

📅  最后修改于: 2023-12-03 14:42:26.635000             🧑  作者: Mango

JavaScript Queue

JavaScript Queue refers to a data structure that follows the First-In-First-Out (FIFO) principle. In other words, the elements that are added first will be removed first.

Types of Queues
  1. Simple Queue
  2. Circular Queue
  3. Priority Queue
Simple Queue

A simple queue is the most basic type of queue in which each element is added at the end of the queue and removed from the front of the queue.

Circular Queue

A circular queue is a type of queue in which the last element is connected to the first element to form a circle. This circular structure allows the queue to avoid wasting space that is present in a simple queue.

Priority Queue

A priority queue is a type of queue that assigns a priority to each element. The element with the highest priority is removed first.

Queue Operations

The following are the basic operations that can be performed on a queue:

  1. Enqueue: Adds an element to the end of the queue.
  2. Dequeue: Removes an element from the front of the queue.
  3. Peek: Returns the front element of the queue without removing it.
  4. IsEmpty: Checks if the queue is empty.
Implementation of Queues in JavaScript

Queues can be implemented in JavaScript in various ways like:

  1. Using Arrays
  2. Using Linked Lists
Using Arrays

Queues can be implemented using arrays by manually manipulating the array indices. Here is an example of how to implement a simple queue using an array:

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

  enqueue(element) {
    this.items.push(element);
  }

  dequeue() {
    if (this.isEmpty()) {
      return "Underflow";
    }
    return this.items.shift();
  }

  peek() {
    if (this.isEmpty()) {
      return "No elements in Queue";
    }
    return this.items[0];
  }

  isEmpty() {
    return this.items.length == 0;
  }
}
Using Linked Lists

Queues can also be implemented using linked lists. Here is an example of how to implement a simple queue using a linked list:

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

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

  enqueue(element) {
    let node = new Node(element);

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

    this.size++;
  }

  dequeue() {
    if (this.front == null) {
      return "Underflow";
    }

    let elementToRemove = this.front;
    this.front = this.front.next;

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

    this.size--;
    return elementToRemove.element;
  }

  peek() {
    if (this.front == null) {
      return "No elements in Queue";
    }
    return this.front.element;
  }

  isEmpty() {
    return this.front == null;
  }
}
Conclusion

JavaScript Queue is an essential data structure that makes it easy to handle elements in the order in which they are added. Queues can be implemented using arrays or linked lists, depending on the use case.