new Queue()
In computer science, a queue (/ˈkjuː/ KYEW) is a particular
kind of abstract data type or collection in which the entities in the
collection are kept in order and the principle (or only) operations on
the collection are the addition of entities to the rear terminal position,
known as enqueue, and removal of entities from the front terminal position,
known as dequeue. This makes the queue a First-In-First-Out (FIFO) data structure.
In a FIFO data structure, the first element added to the queue will be the
first one to be removed. Full wikipedia article at:
https://en.wikipedia.org/wiki/Queue_(abstract_data_type)
Example
const Queue = require('dstructures').Queue;
const myQueue = new Queue();
Methods
dequeue()
Removes and returns the first element in a queue.
Returns:
Returns the removed element if the queue is not empty, otherwise returns false.
Example
[1, 2, 3] Queue.dequeue(); // [2, 3]
[2, 3] Queue.dequeue(); // [3]
enqueue(element)
Adds element in a queue.
Parameters:
Name | Type | Description |
---|---|---|
element |
any
|
Element that will be added. |
Example
[] Queue.enqueue(1); // [1]
[1] Queue.enqueue(2); // [1, 2]
head()
Returns the first element in a queue.
Returns:
Returns the first element if the queue is not empty, otherwise returns false.
Example
[1, 2, 3] Queue.head(); // 1
['Cat', 'Dog', 'Pig'] Queue.head(); // 'Cat'
isEmpty()
Returns true if queue is empty.
Returns:
Returns true if queue is empty, otherwise returns false.
Example
[] Queue.empty(); // true
[1, 2] Queue.empty(); // false
tail()
Returns the last element in a queue.
Returns:
Returns the last element if the queue is not empty, otherwise returns false.
Example
[1, 2, 3] Queue.tail(); // 3
['Cat', 'Dog', 'Pig'] Queue.tail(); // 'Pig'