Implementation of a singly linked list in JavaScript (singly meaning unidirectional and can be traversed in only one direction)
Linked lists are the base structures that other more complex data structures are derived from and store data in a series of "nodes". If you're here you know the differences, and if not, then you should read more about them, their advantages and disadvantages, etc.
git clone https://github.com/ahadb/linked-list
, create a tarball, or use directly or npm below:
NPM:
npm install another-linked-list
Common JS:
const LinkedList = require('another-linked-list')
const singlyLinkedList = new LinkedList();
singlyLinkedList.append(1);
singlyLinkedList.append({ a: 1 });
// etc
ES6 Module (UMD):
import LinkedList from 'another-linked-list';
const singlyLinkedList = new LinkedList();
// objects as nodes
const node1 = { value: 1, key: 'one' };
const node2 = { value: 2, key: 'two' };
singlyLinkedList
.append(node1)
.prepend(node2);
singlyLinkedList.head // => nodeValue1
singlyLinkedList.tail // => nodeValue2
const nodeStringifier = value => `${value.key}:${value.value}`;
singlyLinkedList.toString(nodeStringifier; // => "two:2,one:1"
The building block of Singly Linked List is a node.
const node2 = new LinkedListNode(2);
const node1 = new LinkedListNode(1, node2);
Create a new Singly Linked List with no items.
const singlyLinkedList = new LinkedList();
Prepend a node to your Singly Linked List.
const singlyLinkedList = new LinkedList();
singlyLinkedList.prepend(2);
Append a node to your Singly Linked List.
const singlyLinkedList = new LinkedList();
singlyLinkedList.append(2);
Delete a node by value anywhere in your Singly Linked List.
const singlyLinkedList = new LinkedList();
singlyLinkedList.append(1);
singlyLinkedList.append(2);
singlyLinkedList.append(3);
singlyLinkedList.delete(3);
Delete the head node in your Singly Linked List.
const singlyLinkedList = new LinkedList();
singlyLinkedList.append(1);
singlyLinkedList.append(2);
singlyLinkedList.append(3);
singlyLinkedList.deleteHead();
Delete the tail node in your Singly Linked List.
const singlyLinkedList = new LinkedList();
singlyLinkedList.append(1);
singlyLinkedList.prepend(2);
singlyLinkedList.prepend(3);
singlyLinkedList.deleteTail();
Find a node by value in your Singly Linked List.
const singlyLinkedList = new LinkedList();
singlyLinkedList.append(1);
singlyLinkedList
.append(2)
.append(3);
// you can use a callback as well here
singlyLinkedList.find({ value: 2 });
Convert your Singly Linked List to an Array
const singlyLinkedList = new LinkedList();
// objects as nodes
const node1 = { value: 1, key: 'one' };
const node2 = { value: 2, key: 'two' };
singlyLinkedList
.append(node1)
.prepend(node2);
singlyLinkedList.toArray()
Convert your Singly Linked List to a String
const singlyLinkedList = new LinkedList();
// objects as nodes
const node1 = { value: 1, key: 'one' };
const node2 = { value: 2, key: 'two' };
singlyLinkedList
.append(node1)
.prepend(node2);
singlyLinkedList.toString()