Security News
The Risks of Misguided Research in Supply Chain Security
Snyk's use of malicious npm packages for research raises ethical concerns, highlighting risks in public deployment, data exfiltration, and unauthorized testing.
@datastructures-js/linked-list
Advanced tools
a javascript implementation of LinkedList & DoublyLinkedList
a javascript implementation of LinkedList & DoublyLinkedList.
npm install --save @datastructures-js/linked-list
const { LinkedList, DoublyLinkedList } = require('@datastructures-js/linked-list');
import { LinkedList, DoublyLinkedList } from '@datastructures-js/linked-list';
const linkedList = new LinkedList();
const doublyLinkedList = new DoublyLinkedList();
inserts a node at the beginning of the list.
runtime | params | return |
---|---|---|
O(1) | value: {object} |
in LinkedList: {LinkedListNode}
in DoublyLinkedList: {DoublyLinkedListNode} |
linkedList.insertFirst(1);
const head1 = linkedList.insertFirst(2);
console.log(head1.getValue()); // 2
doublyLinkedList.insertFirst(1);
const head2 = doublyLinkedList.insertFirst(2);
console.log(head2.getValue()); // 2
inserts a node at the end of the list.
runtime | params | return |
---|---|---|
in LinkedList: O(n)
in DoublyLinkedList: O(1) | value: {object} |
in LinkedList: {LinkedListNde}
in DoublyLinkedList: {DoublyLinkedListNode} |
linkedList.insertLast(3);
const last1 = linkedList.insertLast(4);
console.log(last1.getValue()); // 4
console.log(last1.getNext()); // null
doublyLinkedList.insertLast(3);
const last2 = doublyLinkedList.insertLast(4);
console.log(last2.getValue()); // 4
console.log(last2.getPrev().getValue()); // 3
inserts a node at specific position of the list. First (head) node is at position 0.
runtime | params | return |
---|---|---|
in LinkedList: O(n)
in DoublyLinkedList: O(n) |
value: {object}
position: {number} |
in LinkedList: {LinkedListNode}
in DoublyLinkedList: {DoublyLinkedListNode} |
const node1 = linkedList.insertAt(5, 2); // node1.getValue() = 5
const node2 = doublyLinkedList.insertAt(5, 2); // node2.getValue() = 5
Loop on the linked list from beginning to end, and pass each node to the callback.
runtime | params |
---|---|
O(n) | cb: {function(node)} |
linkedList.forEach((node) => console.log(node.getValue()));
/*
2
1
5
3
4
*/
doublyLinkedList.forEach((node) => console.log(node.getValue()));
/*
2
1
5
3
4
*/
Only in DoublyLinkedList. Loop on the doubly linked list from end to beginning, and pass each node to the callback.
runtime | params |
---|---|
O(n) | cb: {function(node)} |
doublyLinkedList.forEachReverse((node) => console.log(node.getValue()));
/*
4
3
5
1
2
*/
returns the first node that returns true from the callback.
runtime | params | return |
---|---|---|
O(n) | cb: {function(node)} |
in LinkedList: {LinkedListNode}
in DoublyLinkedList: {DoublyLinkedListNode} |
const node1 = linkedList.find((node) => node.getValue() === 5);
console.log(node1.getValue()); // 5
console.log(node1.getNext().getValue()); // 3
const node2 = doublyLinkedList.find((node) => node.getValue() === 5);
console.log(node2.getValue()); // 5
console.log(node2.getNext().getValue()); // 3
console.log(node2.getPrev().getValue()); // 1
returns a filtered linked list of all the nodes that returns true from the callback.
runtime | params | return |
---|---|---|
O(n) | cb: {function(node)} |
in LinkedList: {LinkedList}
in DoublyLinkedList: {DoublyLinkedList} |
const filterLinkedList = linkedList.filter((node) => node.getValue() > 2);
filterLinkedList.forEach((node) => console.log(node.getValue()));
/*
5
3
4
*/
const filteredDoublyLinkedList = doublyLinkedList.filter((node) => node.getValue() > 2);
filteredDoublyLinkedList.forEach((node) => console.log(node.getValue()));
/*
5
3
4
*/
converts the linked list into an array.
runtime | return |
---|---|
O(n) | {array} |
console.log(linkedList.toArray()); // [2, 1, 5, 3, 4]
console.log(doublyLinkedList.toArray()); // [2, 1, 5, 3, 4]
returns the head node in the linked list.
runtime | return |
---|---|
O(1) |
in LinkedList: {LinkedListNode}
in DoublyLinkedList: {DoublyLinkedListNode} |
console.log(linkedList.head().getValue()); // 2
console.log(doublyLinkedList.head().getValue()); // 2
Only in DoublyLinkedList. returns the tail node in the doubly linked list
runtime | return |
---|---|
O(1) | {DoublyLinkedListNode} |
console.log(doublyLinkedList.tail().getValue()); // 4
returns the number of nodes in the linked list.
runtime | return |
---|---|
O(1) | {number} |
console.log(linkedList.count()); // 5
console.log(doublyLinkedList.count()); // 5
removes the first (head) node of the list.
runtime | return |
---|---|
O(1) | {boolean} |
linkedList.removeFirst(); // true
doublyLinkedList.removeFirst(); // true
removes the last node from the list.
runtime | return |
---|---|
in LinkedList: O(n)
in DoublyLinkedList: O(1) | {boolean} |
linkedList.removeLast(); // true
doublyLinkedList.removeLast(); // true
removes a node at a specific position. First (head) node is at position 0.
runtime | params | return |
---|---|---|
O(n) | position: {number} | {boolean} |
linkedList.removeAt(1); // true
doublyLinkedList.removeAt(1); // true
Loop on the linked list from beginning to end, removes the nodes that returns true from the callback.
runtime | params | return |
---|---|---|
O(n) | cb: {function(node)} | {number} number of removed nodes |
linkedList.removeEach((node) => node.getValue() > 1); // 1
console.log(linkedList.toArray()); // [1]
doublyLinkedList.removeEach((node) => node.getValue() > 1); // 1
console.log(doublyLinkedList.toArray()); // [1]
remove all nodes in the linked list.
runtime |
---|
O(1) |
linkedList.clear();
console.log(linkedList.count()); // 0
console.log(linkedList.head()); // null
doublyLinkedList.clear();
console.log(linkedList.count()); // 0
console.log(doublyLinkedList.head()); // null
console.log(doublyLinkedList.tail()); // null
grunt build
The MIT License. Full License is here
FAQs
a javascript implementation of LinkedList & DoublyLinkedList
The npm package @datastructures-js/linked-list receives a total of 3,007 weekly downloads. As such, @datastructures-js/linked-list popularity was classified as popular.
We found that @datastructures-js/linked-list demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Snyk's use of malicious npm packages for research raises ethical concerns, highlighting risks in public deployment, data exfiltration, and unauthorized testing.
Research
Security News
Socket researchers found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.