#LinkedList.js A simple doubly linked list implementation in Javascript.
npm install linkedlist-js
require the module...
var List = require('linkedlist-js').List;
instantiate a List object...
var list = new List();
// PUSH!
list.push('HUEHUE');
list.each(function (index, node) {
console.log(index + ': ' + node.value());
});
var node = list.head();
while (node != null) {
console.log(node.value());
node = node.next();
}
Creates a node with the value specified, adds it to the end of the list and returns the Node
object.
var a = list.push('A');
a.value(); //'A'
Creates a node with the value specified, adds it to the beginning of the list and returns the Node
object.
var b = list.unshift('B');
b.value(); //'B'
Returns the tail Node
and removes it from the list.
var list = new List();
list.push('A');
list.push('B');
list.pop().value(); //'B'
Returns the head Node
and removes it from the list.
var list = new List();
list.push('A');
list.push('B');
list.shift().value(); //'A'
Remove a specific Node
(An O(1) complexity action in linked lists)
var list = new List();
list.push('A');
list.push('B');
list.push('C');
var node = list.find('B');
list.removeNode(node);
list.count(); // 2
list.get(0).value(); // 'A'
list.get(1).value(); // 'C';
Returns the Node
at the specified index. (Linear Lookup, not very performant)
var list = new List();
list.push('A');
list.push('B');
list.push('C');
list.get(1).value(); //'B'
Returns the head Node
or null
in list is empty
var list = new List();
var node_a = list.push('A');
var node_b = list.push('B');
list.head() === node_a; // true
Returns the tail Node
or null
in list is empty
var list = new List();
var node_a = list.push('A');
var node_b = list.push('B');
list.tail() === node_b; // true
Find the node with the specified value and returns it. (Linear search, not very performant)
var list = new List();
list.push('A');
var node_b = list.push('B');
list.find('B') === node_b; // true
Sets the value of the Node
at the specifiec index.
var list = new List();
list.push('A');
list.set(0, 'B');
list.head().value(); // 'B'
Returns the number of Node
s in the list.
var list = new List();
list.push('A');
list.push('B');
list.count(); // 2
Returns true
if list has no Node
s and false
if otherwise.
var list = new List();
list.isEmpty(); // true
list.push('A');
list.isEmpty(); // false
Truncates the list to the specified size.
var list = new List();
list.push('A');
list.push('B');
list.truncateTo(1);
list.count(); // 1
Same as calling truncateTo(0)
. Empties the list.
var list = new List();
list.push('A');
list.push('B');
list.empty();
list.count(); // 0
list.isEmpty(); // true
Returns the list as an Array.
var list = new List();
list.push('A');
list.push('B');
list.asArray(); // ['A', 'B']
Returns the value of the Node
Returns the previous Node
in the list of null
if the node is the forst Node
.
Returns the next Node
in the list of null
if the node is the last Node
.
Sets the value of the node
var node = new Node();
node.set('A');
node.value(); // 'A'
Sets the previous pointer of the Node
var node_a = new Node();
var node_b = new Node();
node_b.setPrevious(node_a);
node_b.previous() === node_a; // true
Sets the next pointer of the Node
var node_a = new Node();
var node_b = new Node();
node_a.setNext(node_b);
node_a.next() === node_b; // true
Retunrs true if the node is the first node in the list.
var list = new List();
list.push('A');
list.push('B');
list.head().isHead(); // true
Retunrs true if the node is the last node in the list.
var list = new List();
list.push('A');
list.push('B');
list.tail().isTail(); // true
npm test
if you have mocha installed, you may also run
mocha tests