Queue

Queue

Queue is a linear data structure with a first-in-first-out(FIFO) mode of operation. A Queue does not support random access. Items are added to the back of the Queue, and removed from the front.

Either an array or a linked list could be used to implement a queue. I've picked linked list because removing an element from the front is an O(1) operation, versus an O(n) operation in arrays.

Constructor

new Queue()

Source:

Members

length

Returns the number of items in the queue

Source:

Methods

dequeue() → {any}

Removes item from the front of the queue and returns the removed value.

Runtime: O(1)

Source:

enqueue(item) → {Queue}

Adds item to the back of the queue. Return the Queue instance to allow chaining of commands.

Runtime: O(1)

Parameters:
Name Type Description
item any

to add

Source:
Example
queue.enqueue(10)
   .enqueue(20)
   .enqueue(30)
   .length   // 3