Giter Club home page Giter Club logo

linked-list's Introduction

Linked List

Implementation of a singly linked list in JavaScript (singly meaning unidirectional and can be traversed in only one direction)

Why Linked Lists?

Linked lists are the base structures that other more complex data structures are derived from and store data in a series of "nodes". If you're here you know the differences, and if not, then you should read more about them, their advantages and disadvantages, etc.

Install

git clone https://github.com/ahadb/linked-list, create a tarball, or use directly or npm below:

NPM:

npm install another-linked-list

Use

Common JS:

const LinkedList = require('another-linked-list')

const singlyLinkedList = new LinkedList();
singlyLinkedList.append(1);
singlyLinkedList.append({ a: 1 });
// etc

ES6 Module (UMD):

import LinkedList from 'another-linked-list';

const singlyLinkedList = new LinkedList();

// objects as nodes
const node1 = { value: 1, key: 'one' };
const node2 = { value: 2, key: 'two' };

singlyLinkedList
  .append(node1)
  .prepend(node2);

singlyLinkedList.head // => nodeValue1
singlyLinkedList.tail // => nodeValue2

const nodeStringifier = value => `${value.key}:${value.value}`;
singlyLinkedList.toString(nodeStringifier; // => "two:2,one:1"

API

LinkedListNode

The building block of Singly Linked List is a node.

const node2 = new LinkedListNode(2);
const node1 = new LinkedListNode(1, node2);

LinkedList

Create a new Singly Linked List with no items.

const singlyLinkedList = new LinkedList();

LinkedList.prepend

Prepend a node to your Singly Linked List.

const singlyLinkedList = new LinkedList();
singlyLinkedList.prepend(2);

LinkedList.append

Append a node to your Singly Linked List.

const singlyLinkedList = new LinkedList();
singlyLinkedList.append(2);

LinkedList.delete

Delete a node by value anywhere in your Singly Linked List.

const singlyLinkedList = new LinkedList();

singlyLinkedList.append(1);
singlyLinkedList.append(2);
singlyLinkedList.append(3);

singlyLinkedList.delete(3);

LinkedList.deleteHead

Delete the head node in your Singly Linked List.

const singlyLinkedList = new LinkedList();

singlyLinkedList.append(1);
singlyLinkedList.append(2);
singlyLinkedList.append(3);

singlyLinkedList.deleteHead();

LinkedList.deleteTail

Delete the tail node in your Singly Linked List.

const singlyLinkedList = new LinkedList();

singlyLinkedList.append(1);
singlyLinkedList.prepend(2);
singlyLinkedList.prepend(3);

singlyLinkedList.deleteTail();

LinkedList.find

Find a node by value in your Singly Linked List.

const singlyLinkedList = new LinkedList();

singlyLinkedList.append(1);

singlyLinkedList
  .append(2)
  .append(3);

// you can use a callback as well here
singlyLinkedList.find({ value: 2 });

LinkedList.toArray

Convert your Singly Linked List to an Array

const singlyLinkedList = new LinkedList();

// objects as nodes
const node1 = { value: 1, key: 'one' };
const node2 = { value: 2, key: 'two' };

singlyLinkedList
  .append(node1)
  .prepend(node2);

singlyLinkedList.toArray()

LinkedList.toString

Convert your Singly Linked List to a String

const singlyLinkedList = new LinkedList();

// objects as nodes
const node1 = { value: 1, key: 'one' };
const node2 = { value: 2, key: 'two' };

singlyLinkedList
  .append(node1)
  .prepend(node2);

singlyLinkedList.toString()

linked-list's People

Contributors

ahadb avatar

Stargazers

 avatar

Watchers

 avatar  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.