linkedlist-js v1.3.0
#LinkedList.js A simple doubly linked list implementation in Javascript.
Installation
npm install linkedlist-jsUsage
require the module...
var List = require('linkedlist-js').List;instantiate a List object...
var list = new List();
// PUSH!
list.push('HUEHUE');Traversing the List
with each(callback)
list.each(function (index, node) {
console.log(index + ': ' + node.value());
});like a badass
var node = list.head();
while (node != null) {
console.log(node.value());
node = node.next();
}List API
Adding Nodes
push(value)
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'unshift(value)
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'Accessing Nodes
pop
Returns the tail Node and removes it from the list.
var list = new List();
list.push('A');
list.push('B');
list.pop().value(); //'B'shift
Returns the head Node and removes it from the list.
var list = new List();
list.push('A');
list.push('B');
list.shift().value(); //'A'get(index)
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'Peeking
head()
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; // truetail()
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; // trueSearching
find(value)
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; // trueMisc
set(index, value)
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'count()
Returns the number of Nodes in the list.
var list = new List();
list.push('A');
list.push('B');
list.count(); // 2isEmpty()
Returns true if list has no Nodes and false if otherwise.
var list = new List();
list.isEmpty(); // true
list.push('A');
list.isEmpty(); // falsetruncateTo(length)
Truncates the list to the specified size.
var list = new List();
list.push('A');
list.push('B');
list.truncateTo(1);
list.count(); // 1empty()
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(); // trueasArray()
Returns the list as an Array.
var list = new List();
list.push('A');
list.push('B');
list.asArray(); // ['A', 'B']Node API
Getters
value()
Returns the value of the Node
previous()
Returns the previous Node in the list of null if the node is the forst Node.
next()
Returns the next Node in the list of null if the node is the last Node.
Setters
set(value)
Sets the value of the node
var node = new Node();
node.set('A');
node.value(); // 'A'setPrevious(Node node)
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; // truesetNext(Node node)
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; // trueMisc
isHead()
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(); // trueisTail()
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(); // trueRunning Tests
npm testif you have mocha installed, you may also run
mocha tests