Package Exports
- @datastructures-js/linked-list
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@datastructures-js/linked-list) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
@datastrucures-js/linked-list
elements 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()); // 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.
let n3 = linkedList.find('n3');
console.log(n3.getValue()); // n3
console.log(n3.getNext().getValue()); // n4
.head()
returns the first linkedListNode object in the list.
let 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
Build
grunt build
License
The MIT License. Full License is here