container-doublylist
v0.1.3
Published
DoublyList implementation in JavaScript
Downloads
1,073
Readme
container-doublylist
DoublyList implementation in JavaScript
To manage a list of elements. Best use case: elements are frequently removed from the list. Complexity in O(1) for addition and removal.
Note: Benchmarks seem to show that list iteration is as fast as array iteration on all major browsers.
To instantiate a new list:
var myList = new DoublyList();
To add an element:
var myObjectReference = myList.add(myObject); // add on front by default
// or
var myObjectReference = myList.addFront(myObject);
// or
var myObjectReference = myList.addBack(myObject);
To remove an element:
myList.removeByReference(myObjectReference); // O(1)
// or
myList.remove(myObject); // O(n)
To pop an element:
var myObject = myList.pop(); // pop from front by default
// or
var myObject = myList.popFront();
// or
var myObject = myList.popBack();
To insert an element before the given node:
var myOtherObjectReference = myList.addBefore(myObjectReference, myOtherObject);
To insert an element after the given node:
var myOtherObjectReference = myList.addAfter(myObjectReference, myOtherObject);
To move an element to the beginning:
myList.moveToTheBeginning(myObjectReference);
To move an element to the end:
myList.moveToTheEnd(myObjectReference);
To iterate through the elements:
for (var node = myList.first; node !== null; node = node.next) {
node.object += 1;
}
To apply a treatment on all the elements:
myList.forEach(function (object) {
console.log(object);
});
To convert into an array:
var myArray = myList.toArray();