Giter VIP home page Giter VIP logo

doubly-linked-list-js's Introduction

DoublyLinkedList

This is a Javascipt implementation of a doubly linked list.

I needed a doubly linked list for a project I was doing, and thought I would post the code here in case it was useful for anyone else.

Works in both the browser and node.js.

Synopsis

node.js.

var DLL = require('doubly-linked-list.js');
var list = new DLL.DoublyLinkedList();

list.append('data1');
node = list.append('data2');
list.append('data3');

size = list.size; // 3
node.prev.data; // data1
node.next.data; // data3

node = list.item(1);
data = node.data; // data2
prev = node.prev; // data1
next = node.next; // data3

node = list.head();
data = node.data; // data1

node = list.tail();
data = node.data; // data3

list.prepend('data4');

size = list.size; // 4

node = list.head();
data = node.data; // data4
prev = node.prev; // null
next = node.next; // data1

Browser.

<script src="doubly-linked-list.js"></script>
<script>
    var list = new DLL.DoublyLinkedList();
    // as above
</script>

API

new DoublyLinkedList()

Creates a new DoublyLinkedList. Takes no arguments.

append(data)

Appends a node to the end of the list. Returns the node.

See item(index) for notes on the structure of the node returned.

prepend(data)

Prepends a node to the end of the list. Returns the node.

See item(index) for notes on the structure of the node returned.

item(index)

Returns the node at the specified index. The index starts at 0.

Note: When the node is returned, it looks like this:

{
    // pointers to the previous and next nodes
    prev: node,
    next: node,
    // your data
    data: data
}

This allows you to easily walk the list.

head()

Returns the node at the head of the list. See item(index) for notes on the structure of the node returned.

tail()

Returns the node at the tail of the list. See item(index) for notes on the structure of the node returned.

size()

Returns the size of the list.

remove(index)

Removes the item at the index.

Note: Not currently implemented. Patches (with tests) welcome.

Build status

Build Status

More

https://github.com/playcraft/gamecore.js/blob/master/src/linkedlist.js

doubly-linked-list-js's People

Contributors

andrewrjones avatar

Watchers

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