node's data type: number, string, boolean, null, undefined.
const linkedListFn = require('@datastructures-js/linked-list');
const linkedList = linkedListFn();
.node(value)
creates a linked list node with a given value. The node object exposes the following functions:
- .setNext(node) sets the next linkedListNode object.
- .getNext() gets the next linkedListNode object.
- .setValue(value) sets the value of the node.
- .getValue() gets the value of the node
const n = linkedList.node('new_node');
console.log(n.getValue()); // new_node
.addFirst(value)
adds a node of the given value at the beginning of the list.
linkedList.addFirst('n1');
.addLast(value)
adds a node of the given value at the end of the list.
linkedList.addLast('n4');
.addAfter(value, newValue)
adds a node with a given value after an existing value's node.
try {
linkedList.addAfter('n1', 'n2');
linkedList.addAfter('n33', 'n3');
}
catch (e) {
console.log(e.message); // node n33 not found
}
.addBefore(value, newValue)
adds a node with a given value before an existing value's node.
try {
linkedList.addBefore('n4', 'n3');
linkedList.addBefore('n33', 'n3');
}
catch (e) {
console.log(e.message); // node n33 not found
}
.find(value) finds a node by its value and returns a linked list node object.
const n3 = linkedList.find('n3');
console.log(n3.getValue()); // n3
console.log(n3.getNext().getValue()); // n4
.head()
returns the first linkedListNode object in the list.
const head = linkedList.head();
console.log(head.getValue()); // n1
.traverse(cb)
traverse the linked list and calls cb for each node
linkedList.traverse((n) => { console.log(n.getValue()); });
// n1
// n2
// n3
// n4
.remove(value)
remove the value's node - if exists - from the list.
linkedList.remove('n3');
.removeFirst()
removes the first node in the list.
linkedList.removeFirst(); // n1 removed
.removeLast()
removes the last node in the list.
linkedList.removeLast(); // n4 removed
.toArray()
converts the linkedList to an array
console.log(linkedList.toArray());
// ['n1', 'n2', 'n3', 'n4']
.count()
returns nodes' count in the list.
console.log(linkedList.count()); // 1
.clear()
removes all nodes from the list.
linkedList.clear();
console.log(linkedList.head()); // null
console.log(linkedList.count()); // 0
grunt build
The MIT License. Full License is here