About:
A lightweight implementation for linked list data stucture in Javascript.
Install
You can install it via npm:
npm install light-linkedlist --save
Tests
You can run the tests using:
npm test
Example of Use:
const L = ;const LinkedList = LLinkedList;const Node = LNode;const n = 'There';const list = ;list;list;list;list; // returns node with data 'C'
Available Classes and Methods:
Node Class API
constructor:
Creates a class instance to represent a node. The node should have two properties, 'data' and 'next'. Accept both of these as arguments to the 'Node' constructor, then assign them to the instance as properties 'data' and 'next'. If 'next' is not provided to the constructor, then default its value to be 'null'. Arguments : (Data, Node) Returns: Node Example:
const n = 'There';ndata // 'Hi'nnext // nullconst n2 = 'Hi' n;nnext // returns n
LinkedList Class API
constructor:
Create a class to represent a linked list. When created, a linked list should have no head node associated with it. The LinkedList instance will have one property, 'head', which is a reference to the first node of the linked list. By default 'head' should be 'null'.
Arguments : -
Returns: LinkedList
Example:
const list = ;listhead // null
insertFirst:
Creates a new Node from argument 'data' and assigns the resulting node to the 'head' property. Make sure to handle the case in which the linked list already has a node assigned to the 'head' property. Arguments : (Data) Returns: null Example:
const list = ;list; // List has one node
size:
Returns the number of nodes in the linked list. Arguments : - Returns: integer Example:
const list = ;list;list;list;listsize; // returns 3
getFirst:
Returns the first node of the linked list. Arguments : Returns: Node Example:
const list = ;list;list;list; // returns Node instance with data 'a'
getLast:
Returns the last node of the linked list. Arguments : Returns: Node Example:
const list = ;list;list;list; // returns node with data 'a'
clear:
Empties the linked list of any nodes. Arguments : Returns: Example:
const list = ;list;list;listclear;listsize; // returns 0
removeFirst:
Removes only the first node of the linked list. The list's head should now be the second element. Arguments : Returns: Example:
const list = ;list;list;list;list; // returns node with data 'a'
removeLast:
Removes the last node of the chain Arguments : Returns: Example:
const list = ;list;list;list;listsize; // returns 1list; // returns node with data of 'b'
insertLast:
Removes the last node of the chain Arguments : Data Returns: Example:
const list = ;list;list;list;list; // returns node with data 'C'
getAt:
Returns the node at the provided index Arguments : integer Returns: Node Example:
const list = ;list;list;list;list; // returns node with data 'b'
removeAt:
Removes node at the provided index Arguments : integer Returns: Example:
const list = ;list;list;list;list;list; // returns node with data 'a'
insertAt:
Create an insert a new node at provided index. If index is out of bounds, add the node to the end of the list. Arguments : integer Returns: Example:
const list = ;list;list;list;listlist; // returns node with data 'Hi'
forEach:
Calls the provided function with every node of the chain Arguments : function Returns: Example:
const list = ; list;list;list;list; list;list; // Returns node with data '11'
for...of Loop:
Linked list should be compatible as the subject of a 'for...of' loop Arguments : Returns: Example:
const list = ; list;list;list;list; for let node of list nodedata += 10; node; // returns node with data 11