adt-queue
v0.1.3
Published
This module implements the abstract data type Queue. All the standard Queue operations have been implemented (create, enqueue, dequeue, front & isEmpty).
Downloads
13
Readme
Deprecated
The functionality in this module has been moved to the module abstract-data-types which now contains the Queue as well as other abstract data types. adt-queue will no longer be maintained.
Please go to https://www.npmjs.org/package/abstract-data-types.
adt-queue
This module implements the abstract data type Queue. All the standard Queue operations have been implemented. These are:
- create instantiates an empty queue
- enqueue adds a new item to the back of the queue
- dequeue returns the item at the front of the queue and removes it from the queue
- front returns the item at the front of the queue
- isEmpty determines whether the queue is empty and returns true if it is and false otherwise
Installation
$ npm install adt-queue
Getting Started
The constructor can be added to your code using the require statement.
var Queue = require('adt-queue');
Create
To create an empty queue simply instantiates and adt-queue object.
var q = new Queue();
Enqueue
The enqueue operation adds a new item to the back of the queue. To do this call the enqueue method on a Queue instance passing it the data item you wish to add to the Queue.
q.enqueue({data: 'item'});
Dequeue
The dequeue method returns the item at the front of the queue and removes that item from the queue. To do this call dequeue on a queue object.
var frontItem = q.dequeue();
If dequeue is called on an empty queue, the following error is thrown.
new Error('adt-queue.dequeue(): Tried to dequeue an empty queue!');
Front
The front method returns the item at the front of the queue, but unlike dequeue it does not remove it from the queue.
var frontItem = q.front();
If front is called on an empty queue, the following error is thrown.
new Error('adt-queue.front(): Tried to get the front of an empty queue!');
IsEmpty
The isEmpty method is a boolean function that, when called on a queue, returns true if the queue is empty and false otherwise.
q.isEmpty();