Socket
Book a DemoInstallSign in
Socket

node-linkedlist

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-linkedlist

Double linked list features

npmnpm
Version
0.1.1
Version published
Weekly downloads
26
550%
Maintainers
1
Weekly downloads
 
Created
Source

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

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.

add

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', function(err, listObj) {
    if (err) console.log(err);
    else {
      console.log(listObj.size);
    }
  });

searchBy

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

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

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

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

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

Arguments

  • node (object) - A node object of the same datatype of the list.

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();
...

setPrevioius

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

Arguments

  • No arguments

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

Set the value that is added to the list. This method is used internally so it is fully transparent via ''list.add(...)'' 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()
...
var newNode = Node.Create();
newNode.setValue({message: 'Created new node.'});
node.setNext(newNode);
...

getValue

Get the value that is stored in the standard 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());
...

Keywords

linked list

FAQs

Package last updated on 30 May 2016

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