Data Structures in JavaScript: Queue

Posted by aut0maat10 on May 23, 2018

Just like a stack, a queue is a linear data structure. The difference is that whereas a stack utilizes “last in, first out” operations (LIFO), a queue operates according to a “first in, first out” pattern (FIFO). This is very much like a line at a deli, where the first customer in line gets served first.

Basic Operations

The basic operations of a queue are:

  • Enqueue — places a new element at the back (or “tail”) of the queue
  • Dequeue — retrieves the element at the queue’s front (or “head”)

JavaScript Implementation

Here is a simple implementation of a queue, utilizing a JavaScript class. We added a helper method isEmpty() to check if the queue is empty.

Now we can create our own queue, and enqueue/dequeue elements (in this case: dog names).


CS50: Queues

Wikipedia: Queue (abstract data type)