Structures-Wiz is a JavaScript based npm package for using awesome data structures like Stacks, Queue, LinkedList, PriorityQueues etc.
-
- Algorithms
- Maximum Sum Subarray
- Maximum Product Subarray
- Longest Increasing Subsequence Length
- Longest Common Subsequence Length
- Shortest Unsorted Continuous Subarray Length
- Is Cycle Present in a Graph
- Diameter of a Binary Tree
- Is Sorted List
- Patience Sort
- Merge Sort
- Topological Sort in a Graph
- Is Increasing Triplet Subsequence Present
- Number of Balanced Binary Trees possible of Height 'h'
- Number of Unique Binary Trees with 'n' unique nodes
- Fenwick Tree (Binary Indexed Tree)
- Segment Tree
- Priority Queues
- Stacks
- Queue 🔜
- LinkedList
- Algorithms
Use the npm package manager to install Structures-Wiz.
npm install structures-wiz
List of useful handy algorithms implemented in an optimised manner
Method to find the maximum sum of a subarray from an array. It is implemented using Kadane's Algorithm with a worst case of O(n) time complexity.
import { getMaximumSumSubarray } from 'structures-wiz';
const list = [1,-2,-3,9,7,0,-15,20];
//DEBUGGER OFF
const maxSum = getMaximumSumSubarray(list);
//DEBUGGER ON
const maxSumDebugOn = getMaximumSumSubarray(list,true);
console.log("Max Sum Subarray is :", maxSum);// 21
Method to find the maximum product of a subarray from an array. It is implemented using Dynamic Programming with a worst case of O(n) time complexity.
import { getMaximumProductSubarray } from 'structures-wiz';
const list = [2,3,-2,4];
//DEBUGGER OFF
const maxProd = getMaximumProductSubarray(list);
//DEBUGGER ON
const maxProdDebugOn = getMaximumProductSubarray(list,true);
console.log("Max Product Subarray is :", maxProd);// 6
Method to find the length of the longest common subsequence from an array. It is implemented using Dynamic programming.
import { getLongestCommonSubsequenceLen } from 'structures-wiz';
const text1 = "abcde";
const text2 = "ace";
//DEBUGGER OFF
const longestLen = getLongestCommonSubsequenceLen(text1, text2);
//DEBUGGER ON
const longestLenOn = getLongestCommonSubsequenceLen(text1, text2, true);
console.log("Length of longest common subsequence is :", longestLen);// 3
Method to find the length of the longest increasing subsequence from an array. It is implemented using Dynamic programming and Patience Sort in O(n logn) time complexity.
import { getLongestIncreasingSubsequenceLen } from 'structures-wiz';
const list = [10,9,2,5,3,7,101,18];
const longestLen = getLongestIncreasingSubsequenceLen(list);
console.log("Length of longest increasing subsequence is :", longestLen);// 4
Method to find the length of the shortest unsorted continuous subarray from an input array.
import { getShortestUnsortedSubarray } from 'structures-wiz';
const list = [2,6,4,8,10,9,15];
const shortedSubArrayLen = getShortestUnsortedSubarray(list);
console.log("Length of hortest Unsorted Continuous Subarray is :", shortedSubArrayLen);// 5
Method to find the cycle is to use Topological sort. First we find the inDegree of each node and then add those which are not dependent on any other node to the queue and traverse through the queue until all of each childs are traversed, meanwhile reducing the inDegree and adding to the queue when inDegree is 0.
Param1: Number of nodes Param2: List of edges in an array where each edge is in the format [outEdge, inEdge]
import { isCyclePresentInGraph } from 'structures-wiz';
const edges = [[1,0]];
//Way 1 - Debugger OFF
const isCyclePresent = isCyclePresentInGraph(2, edges);
//Way 1 - Debugger ON
const isCyclePresentDebugON = isCyclePresentInGraph(2, edges, true);
console.log("Is Cycle Present :", isCyclePresent);// false
The diameter of a tree (sometimes called the width) is the number of nodes on the longest path between two end nodes. This path may or may not be passing through the root node.
Params: root of the Tree
Definition for a binary tree node. function TreeNode(val, left, right) { this.val = (val === undefined ? 0 : val) this.left = (left === undefined ? null : left) this.right = (right === undefined ? null : right) }
import { getDiameterOfBinaryTree } from 'structures-wiz';
//Debugging OFF
const diameter = getDiameterOfBinaryTree(root); //Binary Tree - [1,2,3,4,5]
//Debugging ON
const diameterDebugON = getDiameterOfBinaryTree(root, true); //Binary Tree - [1,2,3,4,5]
console.log("Diameter is :", diameter);// 3
Function to check if the list is sorted or not in an ascending order.
import { isSortedList } from 'structures-wiz';
const list = [20,3,1,5,7,2,2,6,99,0];
//Ascending Order - (default)
const isSorted = isSortedList(list);
//Descending Order - pass a comparator as second argument
const isSorted = isSortedList(list, (a,b)=> b-a);
//Debugger ON (3rd Argument)
const isSorted = isSortedList(list, (a,b)=> b-a, true);
console.log("Is List Sorted:", isSorted);// false
Patience sort uses series of increasing stacks like the game of Solitaire to sort a list in worst case of O(n log n ) time complexity. It accepts two parameters first is the list to be sorted, second is for debugger statements to see how the list is being sorted for better understanding.
import { getPatienceSortedList } from 'structures-wiz';
const list = [20,3,1,5,7,2,2,6,99,0];
//Debugging OFF
const sortedList = getPatienceSortedList(list);
console.log("Sorted List is :", sortedList);// [ 0, 1, 2, 2, 3, 5, 6, 7, 20, 99]
//Debugging ON
const sortedDebugList = getPatienceSortedList(list, true);
Merge sort uses the concept of divide-and-conquer to sort the given list of elements. It breaks down the problem into smaller subproblems until they become simple enough to solve directly. Worst case time complexity is O(n logn)
import { getMergeSortedList } from 'structures-wiz';
const list = [20,3,1,5,7,2,2,6,99,0];
//Debugging OFF
const sortedList = getMergeSortedList(list);
//Debugging ON
const sortedDebugList = getMergeSortedList(list, true);
console.log("Sorted List is :", sortedList);// [ 0, 1, 2, 2, 3, 5, 6, 7, 20, 99]
Topologically sort in a graph helps in build algorithms where few packages are dependent on several other packages to get built before them. Topological Sort gives the sequence in which one can build the packages without coming into deadlock situation
import { getTopologicallySortedGraph } from 'structures-wiz';
//[[inEdge, outEdge]] - FORMAT
const edges = [[1,0],[2,0],[3,1],[3,2]];
//DEBUGGER OFF
const topologicallySortedList = getTopologicallySortedGraph(4, edges);
//DEBUGGER ON
const topologicallySortedListDebug = getTopologicallySortedGraph(4, edges, true);
console.log("Topologically Sorted List is :", topologicallySortedList);// [0,1,2,3]
Algorithm to find if an increasing triplet is found in an array or not. The implemenation takes O(N) of Time complexity and O(1) of space complexity.
import { isIncreasingTripletSubsequencePresent } from 'structures-wiz';
const list = [1,2,3,4,5];
const isIncTripletPresent = isIncreasingTripletSubsequencePresent(list);
console.log("Is Increasing Triplet present :", isIncTripletPresent);// true
Algorithm to find maximum number of Balanced Binary Trees of a given height 'h' using Dynamic Programming in O(h) time complexity.
import { getNumberOfBalancedBinaryTrees } from 'structures-wiz';
const h = 2;
const maxBBT = getNumberOfBalancedBinaryTrees(h);
console.log("Max Number of BBT from h:", maxBBT);// 3
Algorithm to find total of unique binary trees from n unique nodes. This implementation calculates nth Catalan Number using Dynamic Programming and Memoization in O(N^2) Time Complexity and O(N) Space Complexity.
import { getUniqueBinaryTreesNum } from 'structures-wiz';
const n = 3;
const uniqBT = getUniqueBinaryTreesNum(n);
console.log("Number of Unique Binary Trees:", uniqBT);// 5
A Fenwick tree or binary indexed tree is a data structure that can efficiently update elements and calculate prefix sums in a table of numbers. Similar to Segment Trees they take O(logN) time to update and fetch the sum.
import { FenwickTree } from 'structures-wiz';
const fTree = new FenwickTree(5);
Following are the methods exposed for usage:
Gets the Fenwick Tree for corresponding input list.
import { FenwickTree } from 'structures-wiz';
const fTree = new FenwickTree(5);
console.log("Tree:",fTree.getTree())
Adds value to the index of the original array.
import { FenwickTree } from 'structures-wiz';
let fTree = new FenwickTree(5);
fTree.add(0,1);
console.log("Tree:",fTree.getTree());
Gets the Sum till the index the created Fenwick Tree.
import { FenwickTree } from 'structures-wiz';
let fTree = new FenwickTree(5);
fTree.add(1,1);
fTree.add(3,1);
console.log(fTree.getSum(4));
console.log("Sum till 4th index:",fTree.getSum(4));
Segment Trees are one of the best data structures for range query operations especially when update operations are freqeuent. Segment Tree creates a Tree with the original array values at its leaf nodes and its sum, min, max values as each of the pair of leaves as its parent. This makes updation take max of O(log N) time complexity and fetch a range value also O(log N).
import { SegmentTree } from 'structures-wiz';
const segTree = new SegmentTree([5,2,1,3,4,6,7,9,8,3]);
Following are the methods exposed for usage:
Gets the Segment Tree for corresponding input list.
import { SegmentTree } from 'structures-wiz';
const segTree = new SegmentTree([5,2,1,3,4,6,7,9,8,3]);
console.log("Tree:",segTree.getTree())
Gets the current state of original array of Segment Tree.
import { SegmentTree } from 'structures-wiz';
const segTree = new SegmentTree([5,2,1,3,4,6,7,9,8,3]);
console.log("List:",segTree.getList())
Gets the Sum of a range from the created Segment Tree.
import { SegmentTree } from 'structures-wiz';
const segTree = new SegmentTree([5,2,1,3,4,6,7,9,8,3]);
//Parameters are lower and upper bound index
console.log("Sum from 0th index to 2nd index:",segTree.getRangeSum(0, 2))
Priority Queues are an extension to queues. Each entry into a Priority Queue is based on its Priority. The advantage of using Priority Queues is that insertion of elements takes O(log n) time because they are implementation is based on Max Heap or Min Heap.
import { PriorityQueue } from 'structures-wiz';
//Default contructor will create a Max-Heap
const priorityQ = new PriorityQueue();
//Passing custom comparator for Min Heap
const priorityQ = new PriorityQueue((a,b) => a <= b);
Following are the methods exposed for usage:
Adds new value to the Queue based on its priority and if Priority is not passed then Priority is equal to Value.
import { PriorityQueue } from 'structures-wiz';
//Default contructor will create a Max-Heap
const priorityQ = new PriorityQueue();
//Way 1 - Without specifying Priority (Priority = Value in this case by default)
priorityQ.enqueue(10);
//Way 2 - Passing Value & Priority
priorityQ.enqueue(10, 100);
Removes the Maximum element (In case of Max-Heap config) or Minimum Element (In case of Min-Heap config).
import { PriorityQueue } from 'structures-wiz';
const priorityQ = new PriorityQueue();
priorityQ.enqueue(10, 100);
priorityQ.enqueue(20, 90);
priorityQ.dequeue(); //Removes the Highest Priority Element
Returns the size of the queue.
import { PriorityQueue } from 'structures-wiz';
const priorityQ = new PriorityQueue();
priorityQ.enqueue(10, 100);
priorityQ.enqueue(20, 90);
priorityQ.getSize(); //2
Returns the left child index of the passed index.
import { PriorityQueue } from 'structures-wiz';
const priorityQ = new PriorityQueue();
priorityQ.enqueue(10, 100);
priorityQ.enqueue(20, 80);
priorityQ.enqueue(60, 90);
priorityQ.enqueue(40, 20);
priorityQ.enqueue(70, 200);
priorityQ.enqueue(50, 40);
const leftIndex = priorityQ.getLeftIndex(0);
console.log("LeftIndex of 0th index is :",leftIndex); //1
Returns the right child index of the passed index.
import { PriorityQueue } from 'structures-wiz';
const priorityQ = new PriorityQueue();
priorityQ.enqueue(10, 100);
priorityQ.enqueue(20, 80);
priorityQ.enqueue(60, 90);
priorityQ.enqueue(40, 20);
priorityQ.enqueue(70, 200);
priorityQ.enqueue(50, 40);
const rightIndex = priorityQ.getRightIndex(0);
console.log("RightIndex of 0th index is :",rightIndex); //2
Returns the parent node index of the passed index.
import { PriorityQueue } from 'structures-wiz';
const priorityQ = new PriorityQueue();
priorityQ.enqueue(10, 100);
priorityQ.enqueue(20, 80);
priorityQ.enqueue(60, 90);
priorityQ.enqueue(40, 20);
priorityQ.enqueue(70, 200);
priorityQ.enqueue(50, 40);
const parentIndex = priorityQ.getParentIndex(1);
console.log("ParentIndex of 1st index is :",parentIndex);//0
Returns the Maximum element (In case of Max-Heap config) or Minimum Element (In case of Min-Heap config).
import { PriorityQueue } from 'structures-wiz';
const priorityQ = new PriorityQueue();
priorityQ.enqueue(10, 100);
priorityQ.enqueue(20, 80);
priorityQ.enqueue(60, 90);
priorityQ.enqueue(40, 20);
priorityQ.enqueue(70, 200);
priorityQ.enqueue(50, 40);
const peekValue = priorityQ.peek();
console.log("Peak Value is :",peekValue); //[ 70 , 200 ]
Returns the current heap
import { PriorityQueue } from 'structures-wiz';
const priorityQ = new PriorityQueue();
priorityQ.enqueue(10, 100);
priorityQ.enqueue(20, 80);
priorityQ.enqueue(60, 90);
priorityQ.enqueue(40, 20);
priorityQ.enqueue(70, 200);
priorityQ.enqueue(50, 40);
const Heap = priorityQ.getHeap();
console.log("Heap is :",Heap);
/*[
[ 70, 200 ],
[ 10, 100 ],
[ 60, 90 ],
[ 40, 20 ],
[ 20, 80 ],
[ 50, 40 ]
]*/
Returns the current heap in the sorted order ( Descending if config is set as Max-Heap else Ascending in case of Min-Heap)
import { PriorityQueue } from 'structures-wiz';
const priorityQ = new PriorityQueue();
priorityQ.enqueue(10, 100);
priorityQ.enqueue(20, 80);
priorityQ.enqueue(60, 90);
priorityQ.enqueue(40, 20);
priorityQ.enqueue(70, 200);
priorityQ.enqueue(50, 40);
const sortedHeap = priorityQ.getSortedHeap();
console.log("Sorted Heap is :",sortedHeap);
/*[
[ 70, 200 ],
[ 10, 100 ],
[ 60, 90 ],
[ 20, 80 ],
[ 50, 40 ],
[ 40, 20 ]
]*/
If the Priority Queue is a Max Heap( by default ) then this method returns 'kth' Max Element in the Sorted Max Heap otherwise in case of Min Heap it returns 'kth' Min Element in the Sorted Min Heap .
import { PriorityQueue } from 'structures-wiz';
const priorityQ = new PriorityQueue();
priorityQ.enqueue(10, 100);
priorityQ.enqueue(20, 80);
priorityQ.enqueue(60, 90);
priorityQ.enqueue(40, 20);
priorityQ.enqueue(70, 200);
priorityQ.enqueue(50, 40);
// kthMax element ( As Max Heap )
const k = 2;
const kthElement = priorityQ.getKth(2);
console.log("kth - ",k," is :",kthElement); //[ 10 , 100 ]
Returns the height of the heap.
import { PriorityQueue } from 'structures-wiz';
const priorityQ = new PriorityQueue();
priorityQ.enqueue(10, 100);
priorityQ.enqueue(20, 80);
priorityQ.enqueue(60, 90);
priorityQ.enqueue(40, 20);
priorityQ.enqueue(70, 200);
priorityQ.enqueue(50, 40);
const height = priorityQ.getHeight();
console.log("Height of Heap is :",height); // 2
Returns true if the node at the index is a leaf node else returns false.
import { PriorityQueue } from 'structures-wiz';
const priorityQ = new PriorityQueue();
priorityQ.enqueue(10, 100);
priorityQ.enqueue(20, 80);
priorityQ.enqueue(60, 90);
priorityQ.enqueue(40, 20);
priorityQ.enqueue(70, 200);
priorityQ.enqueue(50, 40);
const index = 4;
const isIndexLeafNode = priorityQ.isLeafNode(index);
console.log("Is ",index,"Th a leaf node ?",isIndexLeafNode); // true
Returns the list of leaf nodes.
import { PriorityQueue } from 'structures-wiz';
const priorityQ = new PriorityQueue();
priorityQ.enqueue(10, 100);
priorityQ.enqueue(20, 80);
priorityQ.enqueue(60, 90);
priorityQ.enqueue(40, 20);
priorityQ.enqueue(70, 200);
priorityQ.enqueue(50, 40);
const leafNodes = priorityQ.getLeafNodes();
console.log("Leaf Nodes are",leafNodes); // [ [ 60, 90 ], [ 40, 20 ], [ 20, 80 ], [ 50, 40 ] ]
Prints the current state of the queue.
import { PriorityQueue } from 'structures-wiz';
const priorityQ = new PriorityQueue();
priorityQ.enqueue(10, 100);
priorityQ.enqueue(20, 80);
priorityQ.enqueue(60, 90);
priorityQ.enqueue(40, 20);
priorityQ.enqueue(70, 200);
priorityQ.enqueue(50, 40);
priorityQ.print();
/*
[
[ 70, 200 ],
[ 10, 100 ],
[ 60, 90 ],
[ 40, 20 ],
[ 20, 80 ],
[ 50, 40 ]
]
*/
Prints the current heap in the sorted order ( Descending if config is set as Max-Heap else Ascending in case of Min-Heap)
import { PriorityQueue } from 'structures-wiz';
const priorityQ = new PriorityQueue();
priorityQ.enqueue(10, 100);
priorityQ.enqueue(20, 80);
priorityQ.enqueue(60, 90);
priorityQ.enqueue(40, 20);
priorityQ.enqueue(70, 200);
priorityQ.enqueue(50, 40);
priorityQ.printSortedHeap();
/*
[
[ 70, 200 ],
[ 10, 100 ],
[ 60, 90 ],
[ 20, 80 ],
[ 50, 40 ],
[ 40, 20 ]
]
*/
Clears the queue.
import { PriorityQueue } from 'structures-wiz';
const priorityQ = new PriorityQueue();
priorityQ.enqueue(10, 100);
priorityQ.enqueue(20, 80);
priorityQ.enqueue(60, 90);
priorityQ.enqueue(40, 20);
priorityQ.enqueue(70, 200);
priorityQ.enqueue(50, 40);
priorityQ.print();
/*
[
[ 70, 200 ],
[ 10, 100 ],
[ 60, 90 ],
[ 40, 20 ],
[ 20, 80 ],
[ 50, 40 ]
]
*/
priorityQ.clear();
priorityQ.print(); // []
Implementation of stack is pretty straightforward with a few additional features like creating a stack with unique elements and removing duplicates in a stack.
import { Stack } from 'structures-wiz';
//Default contructor will create a Stack with empty elements and "isUnique" flag false
const stack = new Stack();
//Passing custom values and setting the second parameter makes the Stack only accept unique Elements
const uniqueStack = new Stack([20, 90],true);
Following are the methods exposed for usage:
import { Stack } from 'structures-wiz';
const stack = new Stack([20, 80, 10, 90]);
const size = stack.getSize();
console.log("Size of the stack is:",size); // 4
Pushes the received value at the end of the stack.
import { Stack } from 'structures-wiz';
const stack = new Stack();
stack.push(100);
stack.push(100);
stack.push(400);
stack.push(600);
stack.push(100);
stack.push(800);
stack.print() // [ 100, 100, 400, 600, 100, 800 ]
Pushes all the received values at the end of the stack.
import { Stack } from 'structures-wiz';
//Config 1 - Pass set of values
const stack = new Stack();
stack.pushAll( 100, 100, 400, 600, 100, 800 );
stack.print() // [ 100, 100, 400, 600, 100, 800 ]
//Config 2 - Pass Array of values
const stack = new Stack();
stack.pushAll([100, 100, 400, 600, 100, 800]);
stack.print() // [ 100, 100, 400, 600, 100, 800 ]
Returns the top most value.
import { Stack } from 'structures-wiz';
const stack = new Stack();
stack.pushAll( 100, 100, 400, 600, 100, 800 );
const peekValue = stack.peek();
console.log("Peek Value is :",peekValue);//800
Removes and returns the top most element of the stack.
import { Stack } from 'structures-wiz';
const stack = new Stack();
stack.pushAll( 100, 100, 400, 600, 100, 800 );
stack.print(); //[ 100, 100, 400, 600, 100, 800 ]
const poppedValue = stack.pop();
console.log("Popped Value is :",poppedValue);//800
stack.print();//[ 100, 100, 400, 600, 100 ]
Removes the duplicates elements of the stack.
import { Stack } from 'structures-wiz';
const stack = new Stack();
stack.pushAll( 100, 100, 400, 600, 100, 800 );
stack.print(); //[ 100, 100, 400, 600, 100, 800 ]
stack.removeDuplicates();
stack.print();//[ 100, 400, 600, 800 ]
Removes all the elements of the stack.
import { Stack } from 'structures-wiz';
const stack = new Stack();
stack.pushAll( 100, 100, 400, 600, 100, 800 );
stack.print(); //[ 100, 100, 400, 600, 100, 800 ]
stack.clear();
stack.print();//[ ]
Prints the current stack state.
import { Stack } from 'structures-wiz';
const stack = new Stack();
stack.pushAll( 100, 100, 400, 600, 100, 800 );
stack.print(); //[ 100, 100, 400, 600, 100, 800 ]
import { LinkedList } from 'structures-wiz';
//Instantiates the LinkedList with no nodes
const linkedList = new LinkedList();
linkedList.print(); //Linked List is empty
Following are the methods exposed for usage.
import { LinkedList } from 'structures-wiz';
const linkedList = new LinkedList();
linkedList.addAtHead(2);
linkedList.addAtHead(5);
linkedList.addAtTail(15);
linkedList.addAtHead(9);
const size = linkedList.getSize();
console.log("Size of the LinkedList is:",size);//4
Method to add a value at the start of the LinkedList.
import { LinkedList } from 'structures-wiz';
const linkedList = new LinkedList();
linkedList.addAtHead(2);
linkedList.addAtHead(5);
linkedList.print(); // 5 -> 2 -> NULL
Method to add a value at the end of the LinkedList.
import { LinkedList } from 'structures-wiz';
const linkedList = new LinkedList();
linkedList.addAtHead(2);
linkedList.addAtHead(5);
linkedList.addAtTail(15);
linkedList.print(); // 5 -> 2 -> 15 -> NULL
Method to remove the value at the start of the LinkedList.
import { LinkedList } from 'structures-wiz';
const linkedList = new LinkedList();
linkedList.addAtHead(2);
linkedList.addAtHead(5);
linkedList.print(); // 5 -> 2 -> NULL
linkedList.removeAtHead(5);
linkedList.print(); // 2 -> NULL
Method to remove the value at the end of the LinkedList.
import { LinkedList } from 'structures-wiz';
const linkedList = new LinkedList();
linkedList.addAtHead(2);
linkedList.addAtHead(5);
linkedList.addAtTail(15);
linkedList.print(); // 5 -> 2 -> 15 -> NULL
linkedList.removeAtTail(5);
linkedList.print(); // 5 -> 2 -> NULL
Prints the LinkedList.
import { LinkedList } from 'structures-wiz';
const linkedList = new LinkedList();
linkedList.print(); // Linked List is empty
linkedList.addAtHead(2);
linkedList.print(); // 2 -> NULL
linkedList.addAtHead(5);
linkedList.print(); //5 -> 2 -> NULL
linkedList.addAtTail(15);
linkedList.print(); // 5 -> 2 -> 15 -> NULL
linkedList.addAtHead(9);
linkedList.print(); //9 -> 5 -> 2 -> 15 -> NULL
Sorts the LinkedList.
import { LinkedList } from 'structures-wiz';
const linkedList = new LinkedList();
linkedList.addAtHead(22);
linkedList.addAtHead(5);
linkedList.addAtTail(15);
linkedList.print(); //5 -> 22 -> 15 -> NULL
linkedList.sort();
linkedList.print(); //5 -> 15 -> 22 -> NULL
Returns the head node of the LinkedList
import { LinkedList } from 'structures-wiz';
const linkedList = new LinkedList();
linkedList.addAtHead(22);
linkedList.addAtHead(5);
linkedList.addAtTail(15);
linkedList.print(); //5 -> 22 -> 15 -> NULL
const currentHead = linkedList.getHead();
console.log("Head Value:",currentHead.val);//Head Value: 5
Returns the head node of the LinkedList
import { LinkedList } from 'structures-wiz';
const linkedList = new LinkedList();
linkedList.addAtHead(22);
linkedList.addAtHead(5);
linkedList.addAtTail(15);
linkedList.print(); //5 -> 22 -> 15 -> NULL
const currentTail = linkedList.getTail();
console.log("Tail Value:",currentHead.val);//Head Value: 15
Returns the Linked list in array format
import { LinkedList } from 'structures-wiz';
const linkedList = new LinkedList();
linkedList.addAtHead(22);
linkedList.addAtHead(5);
linkedList.addAtTail(15);
linkedList.print(); //5 -> 22 -> 15 -> NULL
const array = linkedList.getArray();
console.log("List converted to Array:",array);//[5, 22, 15]
Clears the LinkedList
import { LinkedList } from 'structures-wiz';
const linkedList = new LinkedList();
linkedList.addAtHead(22);
linkedList.addAtHead(5);
linkedList.addAtTail(15);
linkedList.print(); //5 -> 22 -> 15 -> NULL
linkedList.clear();
linkedList.print()//Linked List is empty
Runs a function passed to all the nodes of linked list and updates the value just like map method of an Array
import { LinkedList } from 'structures-wiz';
function double(node){
node.val *= 2;
}
const linkedList = new LinkedList();
linkedList.addAtHead(22);
linkedList.addAtHead(5);
linkedList.addAtTail(15);
linkedList.print(); //5 -> 22 -> 15 -> NULL
linkedList.mapRunner(double);
linkedList.print()//10 -> 44 -> 30 -> NULL
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.