queuestacklib
v1.0.2
Published
Queue and Stack classes
Downloads
9
Maintainers
Readme
QueueStack
Queue & Stack classes.
You can import the constructors like so:
const queuestacklib = require("queuestacklib");
const Queue = queuestacklib.Queue;
const Stack = queuestacklib.Stack;
// Using ES6 Destructuring Assignment
const {Queue, Stack} = require("queuestacklib");
Queue
Constructor
new Queue( [ iterable = null [, limit = Number.MAX_SAFE_INTEGER ]] );
A bounded Queue which inherits from a Doubly Linked List.
Constructor Parameters
iterable
(Optional) IterableThe values of the optional Iterable will be used to populate the new Queue.
limit
(Optional) NumberA Number which specifies the maximum number of values allowed within the Queue.
Examples
Example 1: Basic Usage:
// Array of arbitrary numbers.
var arr = [1,2,3,4,5];
// New Queue is created using the contents of `arr`.
var queue = new Queue(arr);
// Append "6" to the end of the Queue.
queue.add(6);
// Removes "1" from the beginning of the Queue.
var one = queue.remove();
// Logs 2, 3, 4, 5, 6
for (const num of queue) {
console.log(num);
}
Instance Properties
Queue.size
Number
Queue.size
A number representing the quantity of values within the Queue.
Queue.limit
Number
Queue.limit
A Number which specifies the maximum number of values allowed within the Queue.
Prototype Methods
Queue.prototype.@@iterator
Iterator
Queue[Symbol.iterator]();
An iterator which yields each value in the Queue.
Examples
Example 1: Basic Usage:
// Array of arbitrary numbers.
var arr = [1,2,3,4,5];
// New Queue is created using the contents of `arr`.
var queue = new Queue(arr);
// Logs 1, 2, 3, 4, and 5.
for (const value of queue) console.log(value);
Queue.prototype.clear
Function
Queue.clear();
Removes all elements from the Queue.
Examples
Example 1: Basic Usage:
// Array of arbitrary numbers.
var arr = [1,2,3,4,5];
// New Queue is created using the contents of `arr`.
var queue = new Queue(arr);
// Queue becomes empty.
Queue.clear();
Queue.prototype.item
Function
Queue.item( index );
Returns the vakue at the specified 0-indexed offset from the beginning of the Queue, or null
if it was not found.
Parameters
index
NumberAn offset from the beginning of the Queue, starting at zero, to look for a value at.
Examples
Example 1: Basic Usage:
// Array of arbitrary numbers.
var arr = [1,2,3,4,5];
// New Queue is created using the contents of `arr`.
var queue = new Queue(arr);
// Returns the value at index 4, the last element in Queue, which contains "5".
var five = queue.item(4);
Queue.prototype.add
Function
Queue.add( element );
Inserts a value at the end of the Queue.
Parameters
element
AnyA value to append the Queue with.
Examples
Example 1: Basic Usage:
// Array of arbitrary numbers.
var arr = [1,2,3,4,5];
// New Queue is created using the contents of `arr`.
var queue = new Queue(arr);
// Inserts "6" at the end of the Queue.
queue.add(6);
Queue.prototype.remove
Function
Queue.remove();
Removes and returns an value from the beginning of the Queue.
Examples
Example 1: Basic Usage:
// Array of arbitrary numbers.
var arr = [1,2,3,4,5];
// New Queue is created using the contents of `arr`.
var queue = new Queue(arr);
// Returns "1" and removes it from the beginning of the Queue.
var one = queue.remove();
Queue.prototype.head
Function
Queue.head();
Returns the value at the beginning of the Queue, or null
if the Queue is empty.
Examples
Example 1: Basic Usage:
// Array of arbitrary numbers.
var arr = [1,2,3,4,5];
// New Queue is created using the contents of `arr`.
var queue = new Queue(arr);
// Returns "1".
var one = queue.head();
Queue.prototype.end
Function
Queue.end();
Returns the value at the end of the Queue, or null
if the Queue is empty.
Examples
Example 1: Basic Usage:
// Array of arbitrary numbers.
var arr = [1,2,3,4,5];
// New Queue is created using the contents of `arr`.
var queue = new Queue(arr);
// Returns "5".
var five = queue.last();
Stack
Constructor
new Stack( [ iterable = null [, limit = Number.MAX_SAFE_INTEGER ]] );
A bounded Stack which inherits from a Doubly Linked List.
Constructor Parameters
iterable
(Optional) IterableThe values of the optional Iterable will be used to populate the new Stack.
limit
(Optional) NumberA Number which specifies the maximum number of values allowed within the Stack.
Examples
Example 1: Basic Usage:
// Array of arbitrary numbers.
var arr = [1,2,3,4,5];
// New Stack is created using the contents of `arr`.
var stack = new Stack(arr);
// Append "6" to the end of the Stack.
stack.add(6);
// Removes "6" from the end of the Stack.
var six = stack.remove();
// Logs 1, 2, 3, 4, 5
for (const num of stack) {
console.log(num);
}
Instance Properties
Stack.size
Number
Stack.size
A number representing the quantity of values within the Stack.
Stack.limit
Number
Stack.limit
A Number which specifies the maximum number of values allowed within the Stack.
Prototype Methods
Stack.prototype.@@iterator
Iterator
Stack[Symbol.iterator]();
An iterator which yields each value in the Stack.
Examples
Example 1: Basic Usage:
// Array of arbitrary numbers.
var arr = [1,2,3,4,5];
// New Stack is created using the contents of `arr`.
var stack = new Stack(arr);
// Logs 1, 2, 3, 4, and 5.
for (const value of stack) console.log(value);
Stack.prototype.clear
Function
Stack.clear();
Removes all elements from the Stack.
Examples
Example 1: Basic Usage:
// Array of arbitrary numbers.
var arr = [1,2,3,4,5];
// New Stack is created using the contents of `arr`.
var stack = new Stack(arr);
// Stack becomes empty.
Stack.clear();
Stack.prototype.item
Function
Stack.item( index );
Returns the vakue at the specified 0-indexed offset from the beginning of the Stack, or null
if it was not found.
Parameters
index
NumberAn offset from the beginning of the Stack, starting at zero, to look for a value at.
Examples
Example 1: Basic Usage:
// Array of arbitrary numbers.
var arr = [1,2,3,4,5];
// New Stack is created using the contents of `arr`.
var stack = new Stack(arr);
// Returns the value at index 4, the last element in Stack, which contains "5".
var five = stack.item(4);
Stack.prototype.add
Function
Stack.add( element );
Inserts a value at the end of the Stack.
Parameters
element
AnyA value to append the Stack with.
Examples
Example 1: Basic Usage:
// Array of arbitrary numbers.
var arr = [1,2,3,4,5];
// New Stack is created using the contents of `arr`.
var stack = new Stack(arr);
// Inserts "6" at the end of the Stack.
stack.add(6);
Stack.prototype.remove
Function
Stack.remove();
Removes and returns an value from the end of the Stack.
Examples
Example 1: Basic Usage:
// Array of arbitrary numbers.
var arr = [1,2,3,4,5];
// New Stack is created using the contents of `arr`.
var stack = new Stack(arr);
// Returns "5" and removes it from the end of the Stack.
var one = stack.remove();
Stack.prototype.head
Function
Stack.head();
Returns the value at the beginning of the Stack, or null
if the Stack is empty.
Examples
Example 1: Basic Usage:
// Array of arbitrary numbers.
var arr = [1,2,3,4,5];
// New Stack is created using the contents of `arr`.
var stack = new Stack(arr);
// Returns "1".
var one = stack.head();
Stack.prototype.end
Function
Stack.end();
Returns the value at the end of the Stack, or null
if the Stack is empty.
Examples
Example 1: Basic Usage:
// Array of arbitrary numbers.
var arr = [1,2,3,4,5];
// New Stack is created using the contents of `arr`.
var stack = new Stack(arr);
// Returns "5".
var five = stack.last();