Singly linked, doubly linked, and circular LinkedList classes.
LinkedList
DoubleLinkedList
CircularLinkedList
CircularDoubleLinkedList
ListElement
You can import the list constructors like so:
const listlib = require("listlib");
const LinkedList = listlib.LinkedList;
const DoubleLinkedList = listlib.DoubleLinkedList;
const CircularLinkedList = listlib.CircularLinkedList;
const CircularDoubleLinkedList = listlib.CircularDoubleLinkedList;
// Using ES6 Destructuring Assignment
const {
LinkedList,
DoubleLinkedList,
CircularLinkedList,
CircularDoubleLinkedList
} = require("listlib");
Constructor
new LinkedList( [ iterable = null ] );
Uses ListElement to represent the list structure.
Acyclic Singly Linked List.
-
iterable
(Optional) IterableThe values of the optional Iterable will be used to populate the new LinkedList.
Example 1: Basic Usage:
// Array of arbitrary numbers.
var arr = [1,2,3,4,5];
// New LinkedList is created using the contents of `arr`.
var list = new LinkedList(arr);
// Append "6" to the end of the list, returns a new ListElement.
var six = list.push(6);
// Prepends "7" to the beginning of the list, returns a new ListElement.
var seven = list.unshift(7);
// Moves "7" to the end of the list.
list.pushBack(seven);
// Logs 1, 2, 3, 4, 5, 6, and 7.
for (const element of list) {
console.log(element.payload);
}
ListElement
LinkedList.head
A ListElement which is considered the head/start of the list, and is used to begin enumeration of subsequent ListElements.
ListElement
LinkedList.tail
A ListElement which is considered the tail/end of the list, and is used to end enumeration of preceding ListElements.
Number
LinkedList.size
A number representing the quantity of ListElements within the LinkedList, not counting the head and tail elements.
Boolean
LinkedList.double
Indicates if the LinkedList is doubly linked, linking every ListElement in both directions.
Boolean
LinkedList.circular
Indicates if the LinkedList is circular, linking the head and tail elements.
See the ListElement documentation.
Iterator
LinkedList[Symbol.iterator]( [ ends = false ] );
An iterator which yields each ListElement in the LinkedList, except for the head and tail elements.
-
ends
BooleanIf set to
true
, the iterator will include the head/tail ListElements in iteration. In a circular LinkedList, this will cause the iterator to infinitely loop through the list unless some user-supplied break condition is in place.
Example 1: Basic Usage:
// Array of arbitrary numbers.
var arr = [1,2,3,4,5];
// New LinkedList is created using the contents of `arr`.
var list = new LinkedList(arr);
// Logs 1, 2, 3, 4, and 5.
for (const value of list) console.log(element.payload);
Example 1: ends
Usage:
// Array of arbitrary numbers.
var arr = [1,2,3,4,5];
// New LinkedList is created using the contents of `arr`.
var list = new LinkedList(arr);
// Logs `null`, 1, 2, 3, 4, 5, and `null`.
for (const value of list[Symbol.iterator](true)) console.log(element.payload);
Iterator
LinkedList.values();
An iterator which yields the value of each ListElement in the LinkedList.
Example 1: Basic Usage:
// Array of arbitrary numbers.
var arr = [1,2,3,4,5];
// New LinkedList is created using the contents of `arr`.
var list = new LinkedList(arr);
// Logs 1, 2, 3, 4, and 5.
for (const value of list.values()) console.log(value);
Iterator
LinkedList.elements();
An iterator which yields each ListElement in the LinkedList, except for the head and tail elements.
Example 1: Basic Usage:
// Array of arbitrary numbers.
var arr = [1,2,3,4,5];
// New LinkedList is created using the contents of `arr`.
var list = new LinkedList(arr);
// Logs 1, 2, 3, 4, and 5.
for (const value of list.values()) console.log(value);
Higher-Order Function
LinkedList.forEach( callback );
Calls callback
with the value of each ListElement.
-
callback
FunctionThe callback function to execute for each ListElement value.
-
value
The value of the current ListElement being enumerated over.
-
index
The index (Starting at zero) of the current ListElement being enumerated over.
-
list
The target LinkedList being iterated through.
Example 1: Basic Usage:
// Array of arbitrary numbers.
var arr = [1,2,3,4,5];
// New LinkedList is created using the contents of `arr`.
var list = new LinkedList(arr);
// Logs 1, 2, 3, 4, and 5.
list.forEach(function (value, index, list) {
console.log(value);
});
Function
LinkedList.fromIterable( iterable );
Inserts the values of the Iterable into the LinkedList.
-
iterable
IterableThe iterable to copy values from.
Example 1: Basic Usage:
// Array of arbitrary numbers.
var arr = [1,2,3,4,5];
// New empty LinkedList is created.
var list = new LinkedList();
// Contents of `arr` are inserted into the list.
list.fromIterable(arr);
Function
LinkedLst.coerceElement( value );
Creates a new ListElement using value
. If value
is already a ListElement, it is returned.
-
value
AnyA ListElement, or a value to create a new ListElement with.
Example 1: Basic Usage:
// Arbitrary number.
var numbers = 123;
// Returns a new ListElement with `numbers` assigned to `element.payload`.
var element = LinkedList.coerceElement(numbers);
Function
LinkedList.item( index );
Returns the ListElement at the specified 0-indexed offset from the head element, or null
if it was not found.
-
index
NumberAn offset from the head element, starting at zero, to look for a ListElement at.
Example 1: Basic Usage:
// Array of arbitrary numbers.
var arr = [1,2,3,4,5];
// New LinkedList is created using the contents of `arr`.
var list = new LinkedList(arr);
// Returns the ListElement at index 4, the last element in the list, which contains "5".
var foundElement = list.item(4);
Function
LinkedList.find( value );
Returns the first ListElement encountered that contains a payload matching value
, or null
if one was not found.
-
value
AnyA value to search for in the LinkedList.
Example 1: Basic Usage:
// Array of arbitrary numbers.
var arr = [1,2,3,4,5];
// New LinkedList is created using the contents of `arr`.
var list = new LinkedList(arr);
// Returns the ListElement containing "3".
var foundElement = list.find(3);
Function
LinkedList.includes( value );
Returns true
if a ListElement is found which contains a payload matching value
, or false
if one was not found.
-
value
AnyA value to search for in the LinkedList.
Example 1: Basic Usage:
// Array of arbitrary numbers.
var arr = [1,2,3,4,5];
// New LinkedList is created using the contents of `arr`.
var list = new LinkedList(arr);
// Returns `true`.
var wasFound = list.includes(3);
Function
LinkedList.getPrev( element );
Returns the ListElement located before element
, or null
if it was not found.
-
element
ListElementA ListElement to search for the preceding ListElement of in the LinkedList.
Example 1: Basic Usage:
// Array of arbitrary numbers.
var arr = [1,2,3,4,5];
// New LinkedList is created using the contents of `arr`.
var list = new LinkedList(arr);
// Returns the element containing "2".
var two = list.find(2);
// Returns the ListElement containing "1".
var one = list.getPrev(two);
Function
LinkedList.first();
Returns the element at the beginning of the LinkedList, or null
if the list is empty.
Example 1: Basic Usage:
// Array of arbitrary numbers.
var arr = [1,2,3,4,5];
// New LinkedList is created using the contents of `arr`.
var list = new LinkedList(arr);
// Returns the element containing "1".
var one = list.first();
Function
LinkedList.last();
Returns the element at the end of the LinkedList, or null
if the list is empty.
Example 1: Basic Usage:
// Array of arbitrary numbers.
var arr = [1,2,3,4,5];
// New LinkedList is created using the contents of `arr`.
var list = new LinkedList(arr);
// Returns the element containing "5".
var five = list.last();
Function
LinkedList.clear();
Removes all elements from the LinkedList.
Example 1: Basic Usage:
// Array of arbitrary numbers.
var arr = [1,2,3,4,5];
// New LinkedList is created using the contents of `arr`.
var list = new LinkedList(arr);
// List becomes empty.
list.clear();
Function
LinkedList.concat( list1 [, list2, ..., listN ] );
Concatenates multiple LinkedLists into the callee LinkedList.
-
list1
...listN
ListElementAn argument list of LinkedLists to concatenate.
Example 1: Basic Usage:
// Array of arbitrary numbers.
var arr = [1,2,3,4,5];
// New LinkedLists are created using the contents of `arr`.
var list1 = new LinkedList(arr);
var list2 = new LinkedList(arr);
var list3 = new LinkedList(arr);
// Appends `list2` and `list3` to the end of `list1`.
list1.concat(list2, list3);
// Logs 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5.
for (const element of list) {
console.log(element.payload);
}
Function
LinkedList.remove( element );
Removes and returns an element from the LinkedList.
-
element
ListElementA ListElement object to remove from the LinkedList.
Example 1: Basic Usage:
// Array of arbitrary numbers.
var arr = [1,2,3,4,5];
// New LinkedList is created using the contents of `arr`.
var list = new LinkedList(arr);
// Get the element which contains "3".
var three = list.find(3);
// Removes the element from the list.
list.remove(three);
Function
LinkedList.insertBefore( element, newElement );
Inserts a ListElement before element
-
element
ListElementA ListElement object to prepend with newElement.
-
newElement
AnyA ListElement or arbitrary value to add to the LinkedList before
element
.
Example 1: Basic Usage:
// Array of arbitrary numbers.
var arr = [1,2,3,4,5];
// New LinkedList is created using the contents of `arr`.
var list = new LinkedList(arr);
// Get the element which contains "3".
var three = list.find(3);
// Inserts "2.5" before "3".
var twoPointFive = list.insertBefore(three, 2.5);
Function
LinkedList.insertAfter( element, newElement );
Inserts a ListElement after element
-
element
ListElementA ListElement object to prepend with newElement.
-
newElement
AnyA ListElement or arbitrary value to add to the LinkedList after
element
.
Example 1: Basic Usage:
// Array of arbitrary numbers.
var arr = [1,2,3,4,5];
// New LinkedList is created using the contents of `arr`.
var list = new LinkedList(arr);
// Get the element which contains "3".
var three = list.find(3);
// Inserts "3.5" after "3".
var threePointFive = list.insertAfter(three, 3.5);
Function
LinkedList.prepend( element );
Alias:
unshift
Inserts a ListElement at the beginning of the LinkedList.
-
element
AnyA ListElement or arbitrary value to prepend the LinkedList with.
Example 1: Basic Usage:
// Array of arbitrary numbers.
var arr = [1,2,3,4,5];
// New LinkedList is created using the contents of `arr`.
var list = new LinkedList(arr);
// Inserts "0" at the beginning of the list.
var zero = list.prepend(0);
Function
LinkedList.unshift( element );
An alias of LinkedList.prepend
.
Function
LinkedList.append( element );
Alias:
push
Inserts a ListElement at the end of the LinkedList.
-
element
AnyA ListElement or arbitrary value to append the LinkedList with.
Example 1: Basic Usage:
// Array of arbitrary numbers.
var arr = [1,2,3,4,5];
// New LinkedList is created using the contents of `arr`.
var list = new LinkedList(arr);
// Inserts "6" at the end of the list.
var six = list.append(6);
Function
LinkedList.push( element );
An alias of LinkedList.prototype.append
.
Function
LinkedList.shift();
Removes an element from the beginning of the LinkedList and returns it.
Example 1: Basic Usage:
// Array of arbitrary numbers.
var arr = [1,2,3,4,5];
// New LinkedList is created using the contents of `arr`.
var list = new LinkedList(arr);
// Removes and returns the first element in the list.
var one = list.shift();
Function
LinkedList.pushBack( element );
Moves a ListElement to the end of the LinkedList.
-
element
AnyA ListElement object to move to the end of the LinkedList.
Example 1: Basic Usage:
// Array of arbitrary numbers.
var arr = [1,2,3,4,5];
// New LinkedList is created using the contents of `arr`.
var list = new LinkedList(arr);
// Returns the element containing "2".
var two = list.find(2);
// Moves "2" to the end of the list.
list.pushBack(two);
Function
LinkedList.copyWithin( target [, start = 0 [, end = LinkedList.size ]] );
Shallow copies ListElements to another location in the same LinkedList and returns it, without modifying its size.
-
target
NumberZero based index at which to copy the sequence to. If negative,
target
will be counted from the end. Iftarget
is at or greater thanLinkedList.size
, nothing will be copied. Iftarget
is positioned after start, the copied sequence will be trimmed to fit LinkedList.size. -
start
(Optional) NumberZero based index at which to start copying elements from. If negative,
start
will be counted from the end. Ifstart
is omitted, copyWithin will copy from the start of the LinkedList (defaults to 0). -
end
(Optional) NumberZero based index at which to end copying elements from. Copies up to but not including end. If negative,
end
will be counted from the end.
Example 1: Basic Usage:
// Array of arbitrary numbers.
var arr = [1,2,3,4,5];
// New LinkedList is created using the contents of `arr`.
var list = new LinkedList(arr);
// Copy to index 0 the element at index 3.
list.copyWithin(0, 3, 4);
// Logs 4, 2, 3, 4, 5
for (const element of list) {
console.log(element.payload);
}
// Copy to index 1 all elements from index 3 to the end.
list.copyWithin(1, 3);
// Logs 4, 4, 5, 4, 5
for (const element of list) {
console.log(element.payload);
}
Constructor
new DoubleLinkedList( [ iterable = null ] );
Inherits from LinkedList
Uses ListElement to represent the list structure.
Refer to the LinkedList documentation for member methods & properties.
Circular Doubly Linked List. Elements have references to previous elements, making some operations faster.
-
iterable
(Optional) IterableThe values of the optional Iterable will be used to populate the new CircularDoubleLinkedList.
Example 1: Basic Usage:
// Array of arbitrary numbers.
var arr = [1,2,3,4,5];
// New DoubleLinkedList is created using the contents of `arr`.
var list = new DoubleLinkedList(arr);
// Append "6" to the end of the list, returns a new ListElement.
var six = list.push(6);
// Prepends "7" to the beginning of the list, returns a new ListElement.
var seven = list.unshift(7);
// Moves "7" to the end of the list.
list.pushBack(seven);
// Logs 1, 2, 3, 4, 5, 6, and 7.
for (const element of list) {
console.log(element.payload);
}
Constructor
new CircularLinkedList( [ iterable = null ] );
Inherits from LinkedList
Uses ListElement to represent the list structure.
Refer to the LinkedList documentation for member methods & properties.
Circular Singly Linked List. The tail and head elements are connected to create a cycle. Iterators will infinitely loop through this list unless they are either interrupted or the list structure becomes broken.
-
iterable
(Optional) IterableThe values of the optional Iterable will be used to populate the new CircularDoubleLinkedList.
Example 1: Basic Usage:
// Array of arbitrary numbers.
var arr = [1,2,3,4,5];
// New CircularLinkedList is created using the contents of `arr`.
var list = new CircularLinkedList(arr);
// Append "6" to the end of the list, returns a new ListElement.
var six = list.push(6);
// Prepends "7" to the beginning of the list, returns a new ListElement.
var seven = list.unshift(7);
// Moves "7" to the end of the list.
list.pushBack(seven);
// Repeatedly logs 1, 2, 3, 4, 5, 6, and 7, in an infinite loop.
for (const element of list) {
console.log(element.payload);
}
Constructor
new CircularDoubleLinkedList( [ iterable = null ] );
Inherits from LinkedList
Uses ListElement to represent the list structure.
Refer to the LinkedList documentation for member methods & properties.
Circular Doubly Linked List. Elements have references to previous elements, making some operations faster. The tail and head elements are connected to create a cycle. Iterators will infinitely loop through this list unless they are either interrupted or the list structure becomes broken.
-
iterable
(Optional) IterableThe values of the optional Iterable will be used to populate the new CircularDoubleLinkedList.
Example 1: Basic Usage:
// Array of arbitrary numbers.
var arr = [1,2,3,4,5];
// New CircularDoubleLinkedList is created using the contents of `arr`.
var list = new CircularDoubleLinkedList(arr);
// Append "6" to the end of the list, returns a new ListElement.
var six = list.push(6);
// Prepends "7" to the beginning of the list, returns a new ListElement.
var seven = list.unshift(7);
// Moves "7" to the end of the list.
list.pushBack(seven);
// Logs 1, 2, 3, 4, 5, 6, and 7.
for (const element of list) {
console.log(element.payload);
}
Constructor
new ListElement( [ payload = null ], [ next = null ], [ prev = null ] );
The class used internally by LinkedList to represent an element of the Linked List.
-
payload
(Optional) AnyArbitrary data which is assigned to
ListElement.payload
. -
next
(Optional) ListElementA
ListElement
to refer to as being next the next element. -
prev
(Optional) ListElementA
ListElement
to refer to as being the previous element.
Example 1: Basic Usage
// A Number
var num = 123;
// Creates a new empty LinkedList.
var list = new LinkedList();
// Creates a new ListElement with `num` assigned to `element.payload`
var element = new list.ListElement(num);
// Appends `element` to the end of the list.
list.push(element);
// Removes `element` from the list.
list.remove(element);
Function
ListElement.fromElement( element );
Copies the payload of a ListElement into the callee ListElement.
-
element
ListElementA ListElement to copy the payload from.
Example 1: Basic Usage:
// A source ListElement
var element1 = new LinkedList.ListElement(123);
// A destination ListElement
var element2 = new LinkedList.ListElement();
// Copies the payload of `element1` into `element2`.
element2.fromElement(element1);