LinkedList and DoubleLinkedList implementation in JavaScript
学习项目,参考自:datastructures-js/linked-list
npm i @kartjim/linkedlist
const {
LinkedList,
DoubleLinkedList,
LinkedListNode,
DoubleLinkedListNode,
} = require('@kartjim/linkedlist');
import {
LinkedList,
DoubleLinkedList,
LinkedListNode,
DoubleLinkedListNode,
} from '@kartjim/linkedlist';
constructor
const root = new LinkedList();
inserts a node at the beginning of the list.
time complexity:
root.insertHead(12)
console.log(root.insertHead(11).getCount()) // 11
console.log(root.getCount()) // 2
console.log(root.toArray()) // [11, 12]
add a node at the end of the LinkedList.
time complexity: min
let saveNode = root.insertTail(21);
root.insertTail(22, saveNode);
console.log(root.getCount()) // 4
console.log(root.toArray()) // [11, 12, 21, 22]
add a node at a specify location.
time complexity:
root.insertAt(2, 31)
console.log(root.getCount()) // 5
console.log(root.toArray()) // [11, 12, 31, 21, 22]
get the head node.
console.log(root.getHead().getValue()) // 11
get the nodes count.
console.log(root.getCount()) // 5
check if the likedlist is empty.
console.log(root.isEmpty()) // false
convert linkedlist into Array.
time complexity:
console.log(root.toArray()) // [11, 12, 31, 21, 22]
reverse the linkedlist.
root.reverse();
// [12, 13, 42, 43]
// =>
// [43, 42, 13, 12]
remove a node at the beginnig of the LinkedList.
time complexity:
root.removeHead()
console.log(root.getHead().getValue()) // 12
remove a node at the end of the LinkedList.
time complexity:
root.removeTail()
console.log(root.getCount()) // 3
console.log(root.toArray()) // [12, 31, 21]
remove a node at a specify location.
First (head) node is at position 0.
time complexity:
root.removeAt(1)
console.log(root.getCount()) // 2
console.log(root.toArray()) // [12, 21]
remove nodes based on a callback.
root.insertTail(41); root.insertTail(42);
console.log(root.toArray()) // [12, 21, 41, 42]
root.removeEach(
(n) => n.getValue() < 41 && n.getValue() > 12
)
console.log(root.toArray()) // [12, 41, 42]
Traverses the list from beginning to end. The original root
will not be changed.
const arr = [];
root.forEach((node) => {
const val = node.getValue();
if (val > 40) arr.push(val);
});
console.log(arr) // [41, 42]
find the first node in the linkedlist based on a callback, get null if nothing is found. It also accepts a second param as the starting node to start searching from.
const num1 = root.find((n) => n.getValue() === 41);
const num2 = root.find((n) => n.getValue() === 21);
console.log(num1.getValue()) // 41
console.log(num2) // null
filter the linkedlist based on a callback, return a new LinkedList.
console.log(root.filter((n) => n.getValue() > 40).toArray()) // [41, 42]
map the linkedlist' value based on a callback, return a new LinkedList.
root.toArray() // [12, 41, 42]);
const newRoot = root.map((n) => n % 2);
newRoot.toArray() // [0, 1, 0]
root === newRoot // false
reduce the linkedlist based on a callback, return value.
const newRoot = root.reduce((pre, cur) => cur + pre, 0);
// [12, 41, 42] => 95
Add a node before the head node.
time complexity:
const Twelve = root.find((n) => n.getValue() === 12);
root.insertBefore(Twelve, 1)
console.log(root.toArray()) // [1, 12, 41, 42]
Add a node after the giving node.
time complexity:
const Thirteen = root.find((n) => n.getValue() === 13);
root.insertAfter(Thirteen, 14)
// [1, 12, 13, 41, 42]
// =>
// [1, 12, 13, 14 41, 42]
remove nodes before the giving node.
time complexity:
const FortyOne = root.find((n) => n.getValue() === 41);
root.removeBefore(FortyOne)
// [12, 13, 14, 41, 42, 43]
// =>
// [12, 13, 41, 42, 43]
remove nodes after the giving node.
time complexity:
const Thirteen = root.find((n) => n.getValue() === 13);
root.removeAfter(Thirteen)
// [12, 13, 41, 42, 43]
// =>
// [12, 13, 42, 43]
clear the linkedlist.
root.clear();
console.log(root.getCount()) // 0
console.log(root.getHead()) // null
create a LinkedList from an Array.
const root2 = LinkedList.fromArray([1, 2, 3, 4, 5]);
set the value of the node.
get the value of the node.
set the next node.
get the next node.
checks if node has a next node.
constructor
const root = new DoubleLinkedList();
inserts a node at the beginning of the DoubleLinkedList.
time complexity:
root.insertHead(2)
console.log(root.insertHead(1).getCount()) // 1
console.log(root.getCount()) // 2
console.log(root.toArray()) // [1, 2]
add a node at the end of the DoubleLinkedList.
time complexity:
root.insertTail(3)
console.log(root.getCount()) // 3
console.log(root.toArray()) // [1, 2, 3]
add a node at a specific position.
time complexity:
root.insertAt(0, 0)
root.insertAt(4, 4)
root.insertAt(2, 21)
console.log(root.getCount()) // 6
console.log(root.toArray()) // [0, 1, 21, 2, 3, 4]
get the count of the DoubleLinkedList.
console.log(root.getCount()) // 6
convert the doubleLinkedList into an array.
console.log(root.toArray()) // [0, 1, 21, 2, 3, 4]
check if the DoubleLinkedList is empty.
console.log(root.isEmpty()) // false
get the head of doubleLinkedList.
console.log(root.getHead().getValue()) // 0
get the tail of doubleLinkedList.
console.log(root.getTail().getValue()) // 4
Traverses the list from beginning to end.
const arr = [];
root.forEach((node) => {
const val = node.getValue();
if (val > 9) arr.push(val);
});
console.log(arr) // [21]
remove the head of the DoubleLinkedList.
time complexity:
console.log(root.removeHead().getValue()) // 0
console.log(root.toArray()) // [1, 21, 2, 3, 4]
remove the tail of the DoubleLinkedList.
time complexity:
console.log(root.removeTail().getValue()) // 4
console.log(root.toArray()) // [1, 21, 2, 3]
remove a node at a specific position.
time complexity:
console.log(root.removeAt(2).getValue()) // 2
console.log(root.toArray()) // [1, 21, 3]
find the first node in the DoubleLinkedList based on a callback, get null if nothing is found. It also accepts a second param as the starting node to start searching from.
const num2 = root.find((n) => n.getValue() === 21);
console.log(num2.getValue()) // 21
filter the linkedlist based on a callback, return a new LinkedList.
console.log(root.filter((n) => n.getValue() > 20).toArray()) // [21]
clear the linkedlist.
root.clear();
console.log(root.getCount()) // 0
console.log(root.isEmpty()) // true
create a LinkedList from an Array.
console.log(DoubleLinkedList.fromArray(['a', 'b', 'c', 'd']).toArray())
// ['a', 'b', 'c', 'd']
set the value of the node.
get the value of the node.
set the previous node.
get the previous node.
check if node has a previous node.
set the next node.
get the next node.
check if node has a next node.