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

@datastructures-js/doubly-linked-list

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@datastructures-js/doubly-linked-list

doubly linked list implementation in javascript

  • 1.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
8
increased by166.67%
Maintainers
1
Weekly downloads
 
Created
Source

@datastrucures-js/doubly-linked-list

build:? npm npm npm

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

dll

Usage

const doublyLinkedListFn = require('@datastructures-js/doubly-linked-list');
const dll = doublyLinkedListFn();

API

.node(value)

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

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

.addFirst(value)

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

dll.addFirst('n1');

.addLast(value)

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

dll.addLast('n4');

.addAfter(value, newValue)

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

try {
  dll.addAfter('n1', 'n2');
  dll.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 {
  dll.addBefore('n4', 'n3');
  dll.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 = dll.find('n3');
console.log(n3.getValue()); // n3
console.log(n3.getNext().getValue()); // n4

.head()

returns the first dll node object in the list.

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

.tail()

returns the first dll node object in the list.

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

.traverse(cb)

traverse the dll from beginning to end and calls cb for each node

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

.traverseBackward(cb)

traverse the dll from end to beginning and calls cb for each node

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

.remove(value)

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

dll.remove('n3');

.removeFirst()

removes the first node in the list.

dll.removeFirst(); // n1 removed

.removeLast()

removes the last node in the list.

dll.removeLast(); // n4 removed

.toArray()

converts the dll to an array

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

.count()

returns nodes' count in the list.

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

.clear()

removes all nodes from the list.

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

Build

grunt build

License

The MIT License. Full License is here

Keywords

FAQs

Package last updated on 18 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