Neatly Prepared Mustache

    node-linkedlist

    0.1.4 • Public • Published

    LinkedList

    An implementation of the concept of double linked lists.

    Documentation

    How to use

    Quick example to get an instance of the linked list:

      var LinkedList = require('node-linkedlist')
        , User = require('../Object/User')
        , list = LinkedList.Create(User);
         
        ... 
      var user = User.Create();
      list.add(user, function(err, listObj) {
        ... 
        ... 
      });

    Linked list

    size

    The number of nodes linked to each other in a row.

    Example

    var list = require("node-linkedlist").Create()
    ...
      console.log(list.size);

    setDataType(dataType)

    You are not fixed to use ''LinkedList'' as it is with the internal standard node. You can use it to chain your own business objects too. The only thing you have to do is to extend the standard node object and publish the constructor of you class to the ''LinkedList'' instance. To publish your own node class without inherit from the standard node you only have to implement the methods that are described at the bottom of the documentation under List node

    Arguments

    • dataType (constructor) - The constructor of the class extending the standard node object.
    • return (LinkedList) - The list itself is returned.

    Example

    var LinkedList = require("node-linkedlist")
      , list = LinkedList.Create()
      , User = require('<path>/User');
     
      list.setDataType(User);

    Alternatively you can publish the constructor directly on create the ''LinkedList'' instance.

    var LinkedList = require("node-linkedlist")
      , User = require('<path>/User')
      , list = LinkedList.Create(User);
     
      ...
      ...

    It is important to know that if you publish a constructor to the ''LinkedList'' instance after adding nodes all of them are lost because publishing requires to set a new first node of the published constructor. It is planned to realize a mixed-mode of nodes which have implemented a standard interface.

    setItems(data[, callback])

    Set a list of nodes.

    Arguments

    • items (array) - A list with single items to set as linked list.
    • callback (function) - The callback function with parameter err and listObj.
    • return (listObj) - The LinkedList instance itself.

    Example

    var LinkedList = require("node-linkedlist")
      , list = LinkedList.Create()
      , nodes = [
        {id: 23, ...},
        {id: 54, ...},
        {id: 43, ...},
        {id: 12, ...},
        {id: 87, ...}
      ];
     
      ...
     
      list.setItems(nodes, function(err, listObj) {
        if (err) console.log(err);
        else {
          console.log(listObj.size);
          // will output the number "5"
        }
      });
     

    add(data[, callback])

    Add a new node to the end of the list.

    Arguments

    • data (string) - The data to add to the list. It can be a node object too.
    • callback (function) - The callback function with parameter err and listObj.
    • return (listObj) - The LinkedList instance itself.

    Example

    var LinkedList = require("node-linkedlist")
      , list = LinkedList.Create()
      , node = list.node;
     
      ...
      list.add({"firstName": "Warden"}, function(err, listObj) {
        if (err) console.log(err);
        else {
          console.log(listObj.size);
        }
      });

    searchBy(property, value)

    Search a node by one of its properties. If the list contains extended standard nodes it is required to implement a getter method like ''getFirstName'' or ''getFirstname''.

    Arguments

    • property (string) - The nodes property to search in the value.
    • value (*) - The value to search in the given property.
    • return (node) - The node you searched or null if it can't be found.

    Example

    var LinkedList = require("node-linkedlist")
      , User = require('<path>/User')
      , list = LinkedList.Create(User);
     
      ...
      list.searchBy('firstName', "Warden");

    get(position[, raw])

    Get a node by a given position.

    Arguments

    • position (number) - The position of the node that is wanted. If the position less equal '0' or higher than the list size the first or last node is returned.
    • raw (boolean) - A flag to get the node itself instead of the value only. Default is false to get only the value.
    • return (Node) - The node at the position or first/last node if the position is less/equal 0 or higher than list size.

    Example

    var LinkedList = require("node-linkedlist")
      , list = LinkedList.Create();
     
      ...
      var node = list.get(54, true);

    delete(position)

    Delete a node from given position.

    Arguments

    • position (number) - The position of the node which has to be removed.
    • return (LinkedList) - The list itself is returned.

    Example

    var LinkedList = require("node-linkedlist")
      , list = LinkedList.Create();
     
      ...
      list.delete(54, true);

    first()

    Get the first node of the list.

    Arguments

    • return (node) - The first node in the list.

    Example

    var LinkedList = require("node-linkedlist")
      , list = LinkedList.Create();
     
      ...
      var firstNode = list.first();

    last()

    Get the last node of the list.

    Arguments

    • return (node) - The last node in the list.

    Example

    var LinkedList = require("node-linkedlist")
      , list = LinkedList.Create();
     
      ...
      var firstNode = list.last();

    isStdNode(node)

    Check if a node is an instance of the internal standard node.

    Arguments

    • node (object) - The node that will be compared with the constructor of the standard node.
    • return (boolean) - True if the given node is a standard node. Otherwise false is returned.

    Example

    var LinkedList = require("node-linkedlist")
      , list = LinkedList.Create();
     
      ...
      var node = ...;
      if (list.isStdNode(node)) {
        ...
        ...
      }

    clean()

    Removes all nodes from the list.

    Arguments

    • return (LinkedList) - The list itself is returned.

    Example

    var LinkedList = require("node-linkedlist")
      , list = LinkedList.Create();
     
      ...
      list.clean();

    toArray()

    Converts the list into an array.

    Arguments

    • return (Array) - All nodes in an array.

    Example

    var LinkedList = require("node-linkedlist")
      , list = LinkedList.Create();
     
      ...
      var nodes = list.toArray();
      ...

    Iteration over the list

    To reduce source code and do not lost performance it is recommended to iterate over the list itself instead of use ''toArray'' and loop over this. The ''LinkedList'' has the standard methods implemented you expect from an iterator.

    next()

    Get the next node in the list.

    hasNext()

    Check the existence of a next node.

    previous()

    Get the previous node in the list.

    hasPrevious()

    Check the existence of a previous node.

    Example

    var LinkedList = require("node-linkedlist")
      , list = LinkedList.Create()
     ,  node = null;
      // Traversing forwards 
      for(node = list.first(); list.hasNext(); node = list.next()) {
       ... 
       ... 
      }
      
      // or backwards
      for(node = list.last(); list.hasPrevious(); node.previous()) {
       ... 
       ... 
      }

    List node

    node [Constructor]

    The list node is the standard node object used by the linked list internally if no other node constructor is offered. You can get it via the property 'node' of the linked list object.

    Arguments

    • No arguments

    Example

    var list = require("node-linkedlist").Create()
      , node = list.node;
     
    var newNode = node.Create();

    setNext(nextNode)

    Set another node object as next node to the current one.

    Arguments

    • nextNode (object) - A node which has to be referenced as next node.

    Example

    var LinkedList = require("node-linkedlist")
      , list = LinkedList.Create();
    ... 
    var last = list.last()
      , node = list.node.Create();
     
    node.setValue({
      field1: true, 
      field2: 123, 
      field3: {success: true}, 
      field4: "Everything's fine."
    });
      
    last.setNext(node);
    ...

    next()

    Get the next node that is referenced to the current node.

    Arguments

    • No argument

    Example

    var LinkedList = require("node-linkedlist")
      , list = LinkedList.Create();
    ... 
    var node = list.first()
    ...
    node = node.next();
    ...

    hasNext()

    Check the existence of a next nodes reference.

    Arguments

    • No argument

    Example

    var LinkedList = require("node-linkedlist")
      , list = LinkedList.Create();
    ... 
    var node = list.first()
    ...
    if (node.hasNext())
      node = node.next();
    ...

    setPrevious(previousNode)

    Set another node object as previous node to the current one.

    Arguments

    • previousNode (node) - The node which has to be referenced before current node.

    Example

    var LinkedList = require("node-linkedlist")
      , list = LinkedList.Create()
      , node = list.node;
    ...
    var last = node.Create()
      , newNode = node.Create();
     
    last.setValue({
      field1: true, 
      field2: 123, 
      field3: {success: true}, 
      field4: "Everything's fine."
    });
     
    newNode.setValue("Only a small text string...");
    last.setPrevious(newNode);
    ...

    previous()

    Get the previous node that is referenced to the current node.

    Arguments

    • No argument

    Example

    var LinkedList = require("node-linkedlist")
      , list = LinkedList.Create();
    ... 
    var node = list.last()
    ...
    node = node.previous();
    ...

    hasPrevious()

    Check the existence of a previous nodes reference.

    Arguments

    • No argument

    Example

    var LinkedList = require("node-linkedlist")
      , list = LinkedList.Create();
    ... 
    var node = list.last()
    ...
    if (node.hasPrevious())
      node = node.previous();
    ...

    setValue(value)

    Set the value that has to be added to the list. This method is used internally so it is fully transparent via ''list.add(...)'' if you use the standard node.

    Arguments

    • value (mixed) - The value that has to be put to a list via a node.

    Example

    var LinkedList = require("node-linkedlist")
      , list = LinkedList.Create()
      , Node = list.node;
    ... 
    var node = list.last()
    ...
    var newNode = Node.Create();
    newNode.setValue({message: 'Created new node.'});
    node.setNext(newNode);
    ...

    getValue()

    Get the value that is stored in a node. This method is used internally so it is fully transparent via ''list.get(position)'' if you use the standard node.

    Arguments

    • No argument

    Example

    var LinkedList = require("node-linkedlist")
      , list = LinkedList.Create()
      , Node = list.node;
    ... 
    var node = list.last()
    console.log(node.getValue());
    ...

    Install

    npm i node-linkedlist

    DownloadsWeekly Downloads

    18

    Version

    0.1.4

    License

    LGPL-3.0

    Last publish

    Collaborators

    • svenmuellerssen