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");
Constructor
new Queue( [ iterable = null [, limit = Number.MAX_SAFE_INTEGER ]] );
A bounded Queue which inherits from a Doubly Linked List.
-
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.
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);
}
Number
Queue.size
A number representing the quantity of values within the Queue.
Number
Queue.limit
A Number which specifies the maximum number of values allowed within the Queue.
Iterator
Queue[Symbol.iterator]();
An iterator which yields each value in the Queue.
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);
Function
Queue.clear();
Removes all elements from the Queue.
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();
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.
-
index
NumberAn offset from the beginning of the Queue, starting at zero, to look for a value at.
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);
Function
Queue.add( element );
Inserts a value at the end of the Queue.
-
element
AnyA value to append the Queue with.
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);
Function
Queue.remove();
Removes and returns an value from the beginning of the Queue.
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();
Function
Queue.head();
Returns the value at the beginning of the Queue, or null
if the Queue is empty.
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();
Function
Queue.end();
Returns the value at the end of the Queue, or null
if the Queue is empty.
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();
Constructor
new Stack( [ iterable = null [, limit = Number.MAX_SAFE_INTEGER ]] );
A bounded Stack which inherits from a Doubly Linked List.
-
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.
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);
}
Number
Stack.size
A number representing the quantity of values within the Stack.
Number
Stack.limit
A Number which specifies the maximum number of values allowed within the Stack.
Iterator
Stack[Symbol.iterator]();
An iterator which yields each value in the Stack.
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);
Function
Stack.clear();
Removes all elements from the Stack.
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();
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.
-
index
NumberAn offset from the beginning of the Stack, starting at zero, to look for a value at.
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);
Function
Stack.add( element );
Inserts a value at the end of the Stack.
-
element
AnyA value to append the Stack with.
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);
Function
Stack.remove();
Removes and returns an value from the end of the Stack.
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();
Function
Stack.head();
Returns the value at the beginning of the Stack, or null
if the Stack is empty.
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();
Function
Stack.end();
Returns the value at the end of the Stack, or null
if the Stack is empty.
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();