Giter Site home page Giter Site logo

linked-list's Introduction

@datastrucures-js/linked-list

build:? npm npm npm

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

Linked List

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

linked-list's People

Contributors

eyas-ranjous avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.