A circular, doubly-linked list with some helpful methods.
as an empty list:
const emptyList = LinkedList();
or with some initial values:
const listWithInitialValues = LinkedList([1, 2, 3, 4, 5]);
For inspecting and retrieving:
Retrieve the current list length
- number of items in the list
let currentLength = listInstance.getLength();
Retrieve a specific list item
- index - zero-based index of item to retrieve
- value of item at supplied index
let value = listInstance.at(3);
Retrieve the first list item
- value of item at head of list
let firstValue = listInstance.first();
Retrieve the last list item
- value of item at tail of list
let lastValue = listInstance.last();
Convert the list into an array
- an array containing all list items in order
let asArray = listInstance.toArray();
For adding and removing:
Add a value to the end of the list
- value - value to add to end of list
listInstance.append(3);
Add a value to the beginning of the list
- value - value to add to front of list
listInstance.prepend('foobar');
Replace value of an existing list node
- index - index of target node to update
- value - value to apply to target node
- old value which was just replaced
let previousValue = listInstance.replaceAt(3, 'updated value');
Insert a value into the list (pushes node at target index forward)
- index - insertion point
- value - value of new node being inserted
listInstance.insertAt(3, 'new value');
Remove a node from the list
- index - target index of node to remove
- value from removed node
let removedValue = listInstance.removeAt(5);
Remove the last node from the list
- value from removed tail node
let value = listInstance.pop();
Remove the first node from the list
- value from removed head node
let value = listInstance.shift();
Higher-order methods:
Execute a callback function on each list node, from head to tail
- cbFn - Callback function invoked with each node value
- thisArg (optional) - Optional this binding for callback function
let listInstance = LinkedList([1,2,3]);
listInstance.forEach((value) => console.log(value));
// 1
// 2
// 3
let testObj = {
multiplier: 2
};
function cbFn(value) {
console.log(this.multiplier * value);
}
listInstance.forEach(cbFn, testObj);
// 2
// 4
// 6
Execute a callback on each list node, from tail to head
- param - Callback function invoked with each node value (from tail to head)
- thisArg (optional) - Optional this binding for callback function
let listInstance = LinkedList([1,2,3]);
listInstance.forEachRev((value) => console.log(value));
// 3
// 2
// 1
Locate a list note which satisfies a predicate function
- param - Callback function returning true/false to indicate a match
-
- thisArg (optional) - Optional this binding for callback function
- Value of first matching node, undefined if none found
let listInstance = LinkedList([1,2,3]);
function isEven(value) {
return value%2 == 0;
}
let foundValue = listInstance.find(isEven);
// 2
Locate ALL list nodes which satisfy a predicate function
- param - Callback function returning true/false to indicate a match
- thisArg (optional) - Optional this binding for callback function
- array of matching values
let listInstance = LinkedList([1,2,3,4,5]);
function isEven(value) {
return value%2 == 0;
}
let matches = listInstance.filter(isEven);
// [2, 4]
Test if all nodes satisfy a predicate function
- param - Callback function returning true/false to indicate a match
- thisArg (optional) - Optional this binding for callback function
- true if all nodes satisfied the predicate, false otherwise
let listInstance = LinkedList([1,2,3,4,5]);
function isNumber(value) {
return typeof value == 'number';
}
function isEven(value) {
return value%2 == 0;
}
listInstance.every(isNumber); // true
listInstance.every(isEven); // false
Test if at least one node satisfies a predicate function
- param - Callback function returning true/false to indicate a match
- thisArg (optional) - Optional this binding for callback function
- true if at least one node value satisfies the predicate, false otherwise
let listInstance = LinkedList([1,2,3,4,5]);
function isEven(value) {
return value%2 == 0;
}
function isString(value) {
return typeof value == 'string';
}
listInstance.some(isEven); // true
listInstance.some(isString); //false
Circular Methods:
Traverse a list in forward or backwarks order, picking n elements at a time.
- An object with the following methods
- next(n = 1) - Select the next 'n' items (defaults to 1), wrapping when reaching the end
- prev(n = 1) - Select the previous 'n' items (defaults to 1), wrapping when reaching the start
let listInstance = LinkedList([1,2,3]);
let t = listInstance.traverse();
t.next(); // 1
t.next(); // 2
t.next(); // 3
t.next(); // 1
t.next(3); // [2, 3, 1]
t.prev(); // 3
t.prev(); // 2
t.prev(); // 1
t.prev(); // 3
t.prev(5); // [2, 1, 3, 2, 1]