What is a Queue Data Structure | JavaScript

What is a Queue Data Structure | JavaScript

Queue is a first in, first out (FIFO) data structure in computer science. Imagine a line up at your favourite restaurant that works on first come, first serve basis. The first person to enter the line up, would be the first one to enter the restaurant. The last person to enter the line up, would be the last one to get in the restaurant.

Queues

  • enqueue - add data to the end of the queue (last node)
  • dequeue - remove and return the data from the beginning of the queue (first node)
  • peek - return data from the beginning of the queue (first node)
  • underflow - happens when we are trying to remove (dequeue) data from an empty queue
  • overflow - happens when we are trying to add (enqueue) data to the full queue
  • unbounded queues - queues that don't have a size restriction, though still are limited by the available memory
  • bounded queues - queues that have a size restriction as to how much data (nodes) they can store

Data Types & Structures

Queues are generally implemented using Linked Lists or Arrays.

Queue implementation using a Linked List

class Queue {
  constructor(maxSize = Infinity) {
    this.queue = new LinkedList();
    this.size = 0;
    this.maxSize = maxSize;
  }

  isEmpty() {}

  hasRoom() {}

  enqueue(data) {}

  dequeue() {}
}