List
"List" is a simple double linked list class. This module is easy to use, light, small and has no dependencies, except assert-testing module for testing (not needed for usage).
The module is written in TypeScript and compiled into commonJS.
This is not any high-tech code. It is not dependent on 98761195187413.4 modules and does not offer unseen JavaScript magic. Download this, save an hour of your time and use it to create something awesome and efficient. We #keepItSimple.
Download
You can download the module on GitHub/simple-double-linked-list or using npm/simple-double-linked-list service.
npm install simple-double-linked-list --save
Double linked list - What is that? (skip if you know)
Linked list is a way to store unknown or large quantity of content, just like an array.
List has entirely different structure, then array though. Array is just a chunk of memory with initial point (position [0]). If you want for example 10th element, you know exactly, when to find it (array[10]). But, it is very inefficient when you want to insert or erase elements anywhere except the end. You must move the entire array by one position, which results in linear asymptotic complexity (inefficient). This is where double linked list excels.
Double linked list can insert or delete at any position with constant asymptotic complexity (very efficient). The problem is, that lists elements cannot be accessed randomly. The list must iterate all the elements to find or reach specific node, which results in inefficient linear asymptotic complexity. The is the lists weakness.
Summary: List is very efficient at inserting and deleting elements at any position, but the elements cannot be accessed randomly, like with an array. You would have to iterate thought the nodes and search (don't worry, the List class has search method implemented).
Usage
Import and create new List
You can import the module using import keyword or require function. In TypeScript, the List class is a generic class ("<>" thingies).
import { List } from "simple-double-linked-list";
var list = new List();
let list = new List<string>();
var L = require("simple-double-linked-list")
var list = new L.List();
List iterator
The list may seem assembled very strangely for some programmers unfamiliar with a linked list. Browsing nodes and values can be confusing. That is why List come with class ListIterator, which handles browsing elements very easily.
By calling simple methods, link .Next() or .Previous(), you can explore the List without any knowledge about List structure whatsoever.
Iterator examples:
var list = new List();
list.AddFront(1);
list.AddFront(5);
list.AddFront(6851);
list.AddFront(0);
list.AddFront(666);
var iterator = list.Begin();
iterator.Next();
iterator.ToLast();
var evilNumber = iterator.Value();
iterator.IsAtEnd();
iterator.Next();
iterator.IsAtEnd();
var someNumber = iterator.Value();
Loop all List elements using the iterator:
var list = new List();
for (var i = 0; i < 100; i++)
list.AddFront(i);
for (var item = list.Begin(); !item.IsAtEnd(); item.Next())
console.log(item.Value());
All iterator methods:
iterator.Next();
iterator.Previous();
iterator.ToFirst();
iterator.ToLast();
iterator.Value();
iterator.Size();
iterator.IsAtEnd();
List
Class List is constructed to work closely with its iterator. If you want to insert new values, delete old values or just update current values, you will have to use an iterator to indicate a position.
Working with the List is easy. Here are all implemented methods:
var list = new List();
list.AddFront("textFront");
list.AddBack("textBack");
list.Find("text");
list.InsertAfter("textToInsert", iterator);
list.InsertBefore("textToInsert", iterator);
list.Remove(iterator);
list.Remove(list.Find("valueToDelete"));
list.Update(iterator, "newValue");
list.Empty();
list.Clear();
list.Size();
list.Begin();
list.End();
list.Print();
More examples
There is a testing file included in this module ("simple-double-linked-list/tests/list_tests.ts" for TypeScript or "simple-double-linked-list/dist/tests/list_tests.js" for JavaScript). The test is "not small", so you may find more examples of the the List class there.
Sorry for my English, I hope it's readable.