@datastrucures-js/linked-list
a javascript implementation of LinkedList & DoublyLinkedList.
Linked List
Doubly Linked List
Table of Contents
installnpm install --save @datastructures-js/linked-list API requireconst { LinkedList, DoublyLinkedList } = require ( '@datastructures-js/linked-list' ) ; importimport { LinkedList, DoublyLinkedList } from '@datastructures-js/linked-list' ; Construction Exampleconst linkedList = new LinkedList ( ) ;
const doublyLinkedList = new DoublyLinkedList ( ) ; .insertFirst(value)inserts a node at the beginning of the list.
params
name type
value object
ExamplelinkedList. insertFirst ( 1 ) ;
const head1 = linkedList. insertFirst ( 2 ) ;
console. log ( head1. getValue ( ) ) ;
doublyLinkedList. insertFirst ( 1 ) ;
const head2 = doublyLinkedList. insertFirst ( 2 ) ;
console. log ( head2. getValue ( ) ) ; .insertLast(value)inserts a node at the end of the list.
params
name type
value object
runtime
LinkedList O(n)
DoublyLinkedList O(1)
ExamplelinkedList. insertLast ( 3 ) ;
const last1 = linkedList. insertLast ( 4 ) ;
console. log ( last1. getValue ( ) ) ;
console. log ( last1. getNext ( ) ) ;
doublyLinkedList. insertLast ( 3 ) ;
const last2 = doublyLinkedList. insertLast ( 4 ) ;
console. log ( last2. getValue ( ) ) ;
console. log ( last2. getPrev ( ) . getValue ( ) ) ; .insertAt(value, position)inserts a node at specific position of the list. First (head) node is at position 0.
params
name type
value object
position number
Exampleconst node1 = linkedList. insertAt ( 5 , 2 ) ;
const node2 = doublyLinkedList. insertAt ( 5 , 2 ) ; .forEach(cb)Loop on the linked list from beginning to end, and pass each node to the callback.
params
name type
cb function
ExamplelinkedList. forEach ( ( node ) => console. log ( node. getValue ( ) ) ) ;
doublyLinkedList. forEach ( ( node ) => console. log ( node. getValue ( ) ) ) ;
.forEachReverse(cb)Only in DoublyLinkedList. Loop on the doubly linked list from end to beginning, and pass each node to the callback.
params
name type
cb function
ExampledoublyLinkedList. forEachReverse ( ( node ) => console. log ( node. getValue ( ) ) ) ;
.find(cb)returns the first node that returns true from the callback or null if nothing found.
params
name type
cb function
Exampleconst node1 = linkedList. find ( ( node ) => node. getValue ( ) === 5 ) ;
console. log ( node1. getValue ( ) ) ;
console. log ( node1. getNext ( ) . getValue ( ) ) ;
const node2 = doublyLinkedList. find ( ( node ) => node. getValue ( ) === 5 ) ;
console. log ( node2. getValue ( ) ) ;
console. log ( node2. getNext ( ) . getValue ( ) ) ;
console. log ( node2. getPrev ( ) . getValue ( ) ) ; .filter(cb)returns a filtered list of all the nodes that returns true from the callback.
params
name type
cb function
Exampleconst filterLinkedList = linkedList. filter ( ( node ) => node. getValue ( ) > 2 ) ;
filterLinkedList. forEach ( ( node ) => console. log ( node. getValue ( ) ) ) ;
const filteredDoublyLinkedList = doublyLinkedList. filter ( ( node ) => node. getValue ( ) > 2 ) ;
filteredDoublyLinkedList. forEach ( ( node ) => console. log ( node. getValue ( ) ) ) ;
.toArray()converts the linked list into an array.
Exampleconsole. log ( linkedList. toArray ( ) ) ;
console. log ( doublyLinkedList. toArray ( ) ) ; .isEmpty()checks if the linked list is empty.
Exampleconsole. log ( linkedList. isEmpty ( ) ) ;
console. log ( doublyLinkedList. isEmpty ( ) ) ; .head()returns the head node in the linked list.
Exampleconsole. log ( linkedList. head ( ) . getValue ( ) ) ;
console. log ( doublyLinkedList. head ( ) . getValue ( ) ) ; .tail()returns the tail node of the doubly linked list.
Exampleconsole. log ( doublyLinkedList. tail ( ) . getValue ( ) ) ; .count()returns the number of nodes in the linked list.
Exampleconsole. log ( linkedList. count ( ) ) ;
console. log ( doublyLinkedList. count ( ) ) ; .removeFirst()removes the first (head) node of the list.
return description
boolean true if a node has been removed
ExamplelinkedList. removeFirst ( ) ;
doublyLinkedList. removeFirst ( ) ; .removeLast()removes the last node from the list.
return description
boolean true if a node has been removed
runtime
LinkedList O(n)
DoublyLinkedList O(1)
ExamplelinkedList. removeLast ( ) ;
doublyLinkedList. removeLast ( ) ; .removeAt(position)removes a node at a specific position. First (head) node is at position 0.
params
name type
position number
return description
boolean true if a node has been removed
ExamplelinkedList. removeAt ( 1 ) ;
doublyLinkedList. removeAt ( 1 ) ; .removeEach(cb)Loop on the linked list from beginning to end, removes the nodes that returns true from the callback.
params
name type
cb function
return description
number number of removed nodes
ExamplelinkedList. removeEach ( ( node ) => node. getValue ( ) > 1 ) ;
console. log ( linkedList. toArray ( ) ) ;
doublyLinkedList. removeEach ( ( node ) => node. getValue ( ) > 1 ) ;
console. log ( doublyLinkedList. toArray ( ) ) ; .clear()remove all nodes in the linked list.
ExamplelinkedList. clear ( ) ;
console. log ( linkedList. count ( ) ) ;
console. log ( linkedList. head ( ) ) ;
doublyLinkedList. clear ( ) ;
console. log ( linkedList. count ( ) ) ;
console. log ( doublyLinkedList. head ( ) ) ;
console. log ( doublyLinkedList. tail ( ) ) ; LinkedListNode .getValue()returns the node's value.
.getNext()returns the next connected node or null if it's the last node.
DoublyLinkedListNode .getValue()returns the node's value.
.getPrev()returns the previous connected node or null if it's the first node.
return
DoublyLinkedListNode
.getNext()returns the next connected node or null if it's the last node.
return
DoublyLinkedListNode
Buildgrunt build
LicenseThe MIT License. Full License is here