@datastrucures-js/linked-list
a javascript implementation of LinkedList & DoublyLinkedList.
Linked List |
|
Doubly Linked List |
|
Table of Contents
install
npm install --save @datastructures-js/linked-list
API
require
const { LinkedList, DoublyLinkedList } = require('@datastructures-js/linked-list');
import
import { LinkedList, DoublyLinkedList } from '@datastructures-js/linked-list';
Construction
Example
const linkedList = new LinkedList();
const doublyLinkedList = new DoublyLinkedList();
.insertFirst(value)
inserts a node at the beginning of the list.
params |
---|
name | type |
value | object |
Example
linkedList.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) |
Example
linkedList.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 |
Example
const 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.
Example
linkedList.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.
Example
doublyLinkedList.forEachReverse((node) => console.log(node.getValue()));
.find(cb)
returns the first node that returns true from the callback or null if nothing found.
Example
const 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.
Example
const 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.
Example
console.log(linkedList.toArray());
console.log(doublyLinkedList.toArray());
.isEmpty()
checks if the linked list is empty.
Example
console.log(linkedList.isEmpty());
console.log(doublyLinkedList.isEmpty());
.head()
returns the head node in the linked list.
Example
console.log(linkedList.head().getValue());
console.log(doublyLinkedList.head().getValue());
.tail()
returns the tail node of the doubly linked list.
Example
console.log(doublyLinkedList.tail().getValue());
.count()
returns the number of nodes in the linked list.
Example
console.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 |
Example
linkedList.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) |
Example
linkedList.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 |
Example
linkedList.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.
return | description |
---|
number | number of removed nodes |
Example
linkedList.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.
Example
linkedList.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 |
Build
grunt build
License
The MIT License. Full License is here