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

linked list implementation in javascript

  • 1.0.3
  • 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

node's data type: number, string, boolean, null, undefined.

ll

Usage

const linkedListFn = require('@datastructures-js/linked-list');
const linkedList = linkedListFn();

API

.node(value)

creates a linked list node with a given value. The node object exposes the following functions:

  • .setNext(node) sets the next linkedListNode object.
  • .getNext() gets the next linkedListNode object.
  • .setValue(value) sets the value of the node.
  • .getValue() gets the value of the node
const n = linkedList.node('new_node');
console.log(n.getValue()); // new_node

.addFirst(value)

adds a node of the given value at the beginning of the list.

linkedList.addFirst('n1');

.addLast(value)

adds a node of the given value at the end of the list.

linkedList.addLast('n4');

.addAfter(value, newValue)

adds a node with a given value after an existing value's node.

try {
  linkedList.addAfter('n1', 'n2');
  linkedList.addAfter('n33', 'n3');
}
catch (e) {
  console.log(e.message); // node n33 not found
}

.addBefore(value, newValue)

adds a node with a given value before an existing value's node.

try {
  linkedList.addBefore('n4', 'n3');
  linkedList.addBefore('n33', 'n3');
}
catch (e) {
  console.log(e.message); // node n33 not found
}

.find(value) finds a node by its value and returns a linked list node object.

const n3 = linkedList.find('n3');
console.log(n3.getValue()); // n3
console.log(n3.getNext().getValue()); // n4

.head()

returns the first linkedListNode object in the list.

const head = linkedList.head();
console.log(head.getValue()); // n1

.traverse(cb)

traverse the linked list and calls cb for each node

linkedList.traverse((n) => { console.log(n.getValue()); });
// n1
// n2   
// n3
// n4

.remove(value)

remove the value's node - if exists - from the list.

linkedList.remove('n3');

.removeFirst()

removes the first node in the list.

linkedList.removeFirst(); // n1 removed

.removeLast()

removes the last node in the list.

linkedList.removeLast(); // n4 removed

.toArray()

converts the linkedList to an array

console.log(linkedList.toArray());
// ['n1', 'n2', 'n3', 'n4']

.count()

returns nodes' count in the list.

console.log(linkedList.count()); // 1

.clear()

removes all nodes from the list.

linkedList.clear();
console.log(linkedList.head()); // null
console.log(linkedList.count()); // 0

Build

grunt build

License

The MIT License. Full License is here

Keywords

FAQs

Package last updated on 06 Jul 2018

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