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

circular-linked-list

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

circular-linked-list

Circular singly linked list, heavily based on Singlie by Klaus Sinani.

  • 0.0.4
  • npm
  • Socket score

Version published
Weekly downloads
3
increased by200%
Maintainers
1
Weekly downloads
 
Created
Source

Circular-linked-list

npm version dependencies devDependencies

Circular singly linked list, heavily based on Singlie by Klaus Sinani.

Installation

$ npm install --save circular-linked-list

Usage

$ npm run lint

$ npm run test

$ npm run test:coverage

$ npm run dist

$ npm run deploy
const CircularLinkedList = require("circular-linked-list");

const list = new CircularLinkedList();

list.append("B").prepend("A");

console.log(list.node(0));
// => Node { value: 'A', next: Node { value: 'B', next: [CircularLinkedList] } }

console.log(list.node(0).next);
// => Node { value: 'B', next: Node { value: 'A', next: [CircularLinkedList] } }

console.log(list.node(0).next.next);
// => Node { value: 'A', next: Node { value: 'B', next: [CircularLinkedList] } }

console.log(
  list
    .map(x => `[${x}]`)
    .reverse()
    .toArray()
);
// => [ '[B]', '[A]' ]

API

list.append(value[, value])
  • Return Type: Linked List

Appends one of more nodes to the list.

value
  • Type: any

Can be one or more comma delimited values. Each value corresponds to a single node.

list.append("A", "B", "C", "D");
// => { value: 'A', next: { value: 'B', next: [List] } }
list.prepend(value[, value])
  • Return Type: Linked List

Prepends one of more nodes to the list.

value
  • Type: any

Can be one or more comma delimited values. Each value corresponds to a single node.

list.append("C", "D");
// => { value: 'C', next: [List] }

list.prepend("B", "A");
// => { value: 'A', next: { value: 'B', next: { value: 'C', next: [List] } } }
list.head
  • Return Type: any

Returns the value of the first node / head on the list.

list.append("A", "B");
console.log(list.head);
// => A
list.last
  • Return Type: any

Returns the value of the last node on the list.

list.append("A", "B");
console.log(list.last);
// => B
list.length
  • Return Type: Integer

Returns the length of the list.

list.append("A", "B");
console.log(list.length);
// => 2
list.isEmpty()
  • Return Type: Boolean

Checks whether or not the list is empty.

list.append("A", "B");
console.log(list.isEmpty());
// => false
list.insert({value[, value], index})
  • Return Type: Linked List

Inserts one or more nodes to the given index.

value
  • Type: any

Can be one or more comma delimited values. Each value corresponds to a single node.

index
  • Type: Integer

Can be an integer corresponding to a list index.

list.append("A", "B", "E");
list.insert({ value: ["C", "D"], index: 1 });
// => { value: 'A', next: { value: 'D', next: { value: 'C', next: { value: 'B', next: [List] } } } }
list.node(index)
  • Return Type: Node

Return the node corresponding to the given index.

index
  • Type: Integer

Can be an integer corresponding to a list index.

list.append("A", "B", "C", "D");
const node = list.node(0);
console.log(node);
// => { value: 'A', next: { value: 'B', next: [List] } }

console.log(node.value);
// => A

console.log(node.next);
// => { value: 'B', next: [List] }
list.get(index)
  • Return Type: any

Return the value of node corresponding to the given index.

index
  • Type: Integer

Can be an integer corresponding to a list index.

list.append("A", "B");
console.log(list.get(0));
// => A

console.log(list.get(0));
// => B
list.remove(index)
  • Return Type: Linked List

Removes from the list the node located to the given index.

index
  • Type: Integer
  • Default: list.length - 1

Can be an integer corresponding to a list index.

If not provided, the last node of the list will be removed.

list.append("A", "B", "C", "D");
// => { value: 'A', next: [List] }

list.remove(0);
// => { value: 'B', next: [List] }

list.remove(0);
// => { value: 'C', next: [List] }
list.toArray()
  • Return Type: Array

Converts the list into an array.

list.append("A", "B", "C");
// => { value: 'A', next: { value: 'B', next: [List] } }

const array = list.toArray();
console.log(array);
// => [ 'A', 'B', 'C' ]
list.clear()
  • Return Type: Empty Linked List

Removes all nodes from the list.

list.append("A", "B", "C");
// => { value: 'A', next: { value: 'B', next: [List] } }

list.clear();
// => null
list.join([separator])
  • Return Type: String

Joins the values of all nodes on the list into a string and returns the string.

separator
  • Type: String
  • Default: Comma ','

Specifies a string to separate each pair of adjacent node values of the array.

If omitted, the node values are separated with a comma ','.

list.append("A", "B", "C");
// => { value: 'A', next: { value: 'B', next: [List] } }

console.log(list.join());
// => 'A,B,C'

console.log(list.join(""));
// => 'ABC'

console.log(list.join(" "));
// => 'A B C'
list.forEach(function)
  • Return Type: undefined

Executes a provided function once for each node value.

function
  • Type: Function

Function to execute for each node value.

const array = [];
list.append("A", "B", "C");
// => { value: 'A', next: { value: 'B', next: [List] } }

list.forEach(x => array.push(x));
console.log(array);
// => [ 'A', 'B', 'C' ];
list.map(function)
  • Return Type: Linked List

Executes a provided function once for each node value.

function
  • Type: Function

Function that produces a new node value for the new list.

list.append("A", "B", "C");
// => { value: 'A', next: { value: 'B', next: [List] } }

const mapped = list.map(x => `[${x}]`);
console.log(array.join(" "));
// => '[A] [B] [C]'

Licence

My work is released under the MIT licence. One can consider this project to be a fork of Singlie by Klaus Sinani.

FAQs

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