@datastrucures-js/linked-list

node's data type: number, string, boolean, null, undefined.
Usage
const linkedListFn = require('@datastructures-js/linked-list');
const linkedList = linkedListFn();
API
.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());
.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);
}
.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);
}
.find(value)
finds a node by its value and returns a linked list node object.
const n3 = linkedList.find('n3');
console.log(n3.getValue());
console.log(n3.getNext().getValue());
.head()
returns the first linkedListNode object in the list.
const head = linkedList.head();
console.log(head.getValue());
.traverse(cb)
traverse the linked list and calls cb for each node
linkedList.traverse((n) => { console.log(n.getValue()); });
.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();
.removeLast()
removes the last node in the list.
linkedList.removeLast();
.toArray()
converts the linkedList to an array
console.log(linkedList.toArray());
.count()
returns nodes' count in the list.
console.log(linkedList.count());
.clear()
removes all nodes from the list.
linkedList.clear();
console.log(linkedList.head());
console.log(linkedList.count());
Build
grunt build
License
The MIT License. Full License is here