Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@datastructures-js/linked-list

Package Overview
Dependencies
Maintainers
1
Versions
28
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@datastructures-js/linked-list

a javascript implementation of LinkedList & DoublyLinkedList

  • 4.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3.8K
decreased by-1.97%
Maintainers
1
Weekly downloads
 
Created
Source

@datastrucures-js/linked-list

build:? npm npm npm

a javascript implementation of LinkedList & DoublyLinkedList.

Linked List Linked List
Doubly Linked List Doubly Linked List

Table of Contents

install

npm install --save @datastructures-js/linked-list

require

const {
  LinkedList,
  DoublyLinkedList,
} = require('@datastructures-js/linked-list');

// list node classes are also exported
const {
  LinkedListNode,
  DoublyLinkedListNode
} = require('@datastructures-js/linked-list');

import

import {
  LinkedList,
  DoublyLinkedList
} from '@datastructures-js/linked-list';

// list node classes are also exported
import {
  LinkedListNode,
  DoublyLinkedListNode
} from '@datastructures-js/linked-list';

API

Construction

const linkedList = new LinkedList();

const doublyLinkedList = new DoublyLinkedList();

.insertFirst(value)

inserts a node at the beginning of the list.

paramsreturnruntime
value: anyLinkedList | DoublyLinkedListO(1)
console.log(linkedList.insertFirst(2).head().getValue()); // 2

console.log(linkedList.insertFirst(1).head().getValue()); // 1

.insertLast(value)

inserts a node at the end of the list.

paramsreturnruntime
value: anyLinkedList | DoublyLinkedList LinkedList: O(n)
DoublyLinkedList: O(1)
linkedList.insertLast(3);
const last1 = linkedList.insertLast(4).find(4);
console.log(last1.getValue()); // 4
console.log(last1.getNext()); // null

doublyLinkedList.insertLast(3);
const last2 = doublyLinkedList.insertLast(4).find(4);
console.log(last2.getValue()); // 4
console.log(last2.getPrev().getValue()); // 3

.insertAt(position, value)

inserts a node at specific position of the list. First (head) node is at position 0.

paramsreturnruntime
position: number
value: any
LinkedList | DoublyLinkedList O(n)
const node1 = linkedList.insertAt(2, 5).find(5); // node1.getValue() = 5

const node2 = doublyLinkedList.insertAt(2, 5).find(5); // node2.getValue() = 5

.forEach(cb)

Loop on the linked list from beginning to end, and pass each node to the callback.

paramsruntime
cb: function O(n)
linkedList.forEach((node, position) => console.log(node.getValue(), position));
/*
2 0
1 1
5 2
3 3
4 4
*/

doublyLinkedList.forEach((node, position) => console.log(node.getValue(), position));
/*
2 0
1 1
5 2
3 3
4 4
*/

.forEachReverse(cb)

Only in DoublyLinkedList. Loop on the doubly linked list from end to beginning, and pass each node to the callback.

paramsruntime
cb: function O(n)
doublyLinkedList.forEachReverse((node, position) => console.log(node.getValue(), position));
/*
4 4
3 3
5 2
1 1
2 0
*/

.find(cb)

returns the first node that return true from the callback or null if nothing found.

paramsreturnruntime
cb: function LinkedListNode | DoublyLinkedListNode O(n)
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

.filter(cb)

returns a filtered linked list of all the nodes that returns true from the callback.

paramsreturnruntime
cb: function LinkedList | DoublyLinkedList O(n)
const filterLinkedList = linkedList.filter((node, position) => node.getValue() > 2);
filterLinkedList.forEach((node) => console.log(node.getValue()));
/*
5
3
4
*/

const filteredDoublyLinkedList = doublyLinkedList.filter((node, position) => node.getValue() > 2);
filteredDoublyLinkedList.forEach((node) => console.log(node.getValue()));
/*
5
3
4
*/

.toArray()

converts the linked list into an array.

returnruntime
array O(n)
console.log(linkedList.toArray()); // [2, 1, 5, 3, 4]

console.log(doublyLinkedList.toArray()); // [2, 1, 5, 3, 4]

.isEmpty()

checks if the linked list is empty.

returnruntime
boolean O(1)
console.log(linkedList.isEmpty()); // false

console.log(doublyLinkedList.isEmpty()); // false

.head()

returns the head node in the linked list.

returnruntime
LinkedListNode | DoublyLinkedListNode O(1)
console.log(linkedList.head().getValue()); // 2

console.log(doublyLinkedList.head().getValue()); // 2

.tail()

returns the tail node of the doubly linked list.

returnruntime
DoublyLinkedListNode O(1)
console.log(doublyLinkedList.tail().getValue()); // 4

.count()

returns the number of nodes in the linked list.

returnruntime
number O(1)
console.log(linkedList.count()); // 5

console.log(doublyLinkedList.count()); // 5

.removeFirst()

removes the first node in the list.

returnruntime
LinkedListNode | DoublyLinkedListNode O(1)
linkedList.removeFirst();

doublyLinkedList.removeFirst();

.removeLast()

removes and returns the last node in the list.

returnruntime
LinkedListNode | DoublyLinkedListNode LinkedList: O(n)
DoublyLinkedList: O(1)
linkedList.removeLast();

doublyLinkedList.removeLast();

.removeAt(position)

removes and returns the node at a specific position. First (head) node is at position 0.

paramsreturnruntime
position: number LinkedListNode | DoublyLinkedListNode O(1)
linkedList.removeAt(1);

doublyLinkedList.removeAt(1);

.removeEach(cb)

Loop on the linked list from beginning to end, removes the nodes that returns a list of the removed nodes.

paramsreturnruntime
cb: function array<LinkedListNode | DoublyLinkedListNode> O(n)
linkedList.removeEach((node, position) => node.getValue() > 1);
console.log(linkedList.toArray()); // [1]

doublyLinkedList.removeEach((node, position) => node.getValue() > 1);
console.log(doublyLinkedList.toArray()); // [1]

.clear()

clears 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

LinkedListNode

new LinkedListNode(value, next)
params
value: any
next: LinkedListNode
.setValue(value)

sets the node's value.

params
value: any
.getValue()

returns the node's value.

return
any
.setNext(next)

sets the node's next connected node.

params
next: LinkedListNode
.getNext()

returns the next connected node or null if it's the last node.

return
LinkedListNode

DoublyLinkedListNode

new DoublyLinkedListNode(value, prev, next)
params
value: any
prev: DoublyLinkedListNode
next: DoublyLinkedListNode
.setValue(value)

sets the node's value.

params
value: any
.getValue()

returns the node's value.

return
any
.setPrev(prev)

sets the node's previous connected node.

params
prev: DoublyLinkedListNode
.getPrev()

returns the previous connected node or null if it's the first node.

return
DoublyLinkedListNode
.setNext(next)

sets the node's next connected node.

params
next: 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

Keywords

FAQs

Package last updated on 16 Feb 2021

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc