Linked list in JavaScript
npm install another-linked-listMy 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 or whatever your preferred method is
To be distributed on NPM.
``javascript
import LinkedList from "./linked-list.js";
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.
`javascript`
const node2 = new LinkedListNode(2);
const node1 = new LinkedListNode(1, node2);
Create a new Singly Linked List with no items.
`javascript`
const singlyLinkedList = new LinkedList();
Prepend a node to your Singly Linked List.
`javascript`
const singlyLinkedList = new LinkedList();
singlyLinkedList.prepend(2);
Append a node to your Singly Linked List.
`javascript`
const singlyLinkedList = new LinkedList();
singlyLinkedList.append(2);
Delete a node by value anywhere in your Singly Linked List.
`javascript
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.
`javascript
const singlyLinkedList = new LinkedList();
singlyLinkedList.append(1);
singlyLinkedList.append(2);
singlyLinkedList.append(3);
singlyLinkedList.deleteHead();
`
Delete the tail node in your Singly Linked List.
`javascript
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.
`javascript
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
`javascript
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
`javascript
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()
``