Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

An optimised 🚀 implementation of Data structures & Algorithms like Fenwick Trees, Segment Trees, Stacks, Priority Queues, Linked Lists etc for enterprise usage in our favourite ❤️ language - JavaScript

NotificationsYou must be signed in to change notification settings

harsh07bharvada/structures-wiz

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

70 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Structures-wiz

Structures-Wiz

GitHub forksGitHub Repo starsnpmGitHub licensenpm

Structures-Wiz is a JavaScript based npm package for using awesome data structures like Stacks, Queue, LinkedList, PriorityQueues etc.

Table of contents

Installation

Use thenpm package manager to install Structures-Wiz.

npm install structures-wiz

Usage

Algorithms

List of useful handy algorithms implemented in an optimised manner

Maximum Sum Subarray

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';constlist=[1,-2,-3,9,7,0,-15,20];//DEBUGGER OFFconstmaxSum=getMaximumSumSubarray(list);//DEBUGGER ONconstmaxSumDebugOn=getMaximumSumSubarray(list,true);console.log("Max Sum Subarray is :",maxSum);// 21

Maximum Product Subarray

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';constlist=[2,3,-2,4];//DEBUGGER OFFconstmaxProd=getMaximumProductSubarray(list);//DEBUGGER ONconstmaxProdDebugOn=getMaximumProductSubarray(list,true);console.log("Max Product Subarray is :",maxProd);// 6

Longest Common Subsequence Length

Method to find the length of the longest common subsequence from an array.It is implemented using Dynamic programming.

import{getLongestCommonSubsequenceLen}from'structures-wiz';consttext1="abcde";consttext2="ace";//DEBUGGER OFFconstlongestLen=getLongestCommonSubsequenceLen(text1,text2);//DEBUGGER ONconstlongestLenOn=getLongestCommonSubsequenceLen(text1,text2,true);console.log("Length of longest common subsequence is :",longestLen);// 3

Longest Increasing Subsequence Length

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';constlist=[10,9,2,5,3,7,101,18];constlongestLen=getLongestIncreasingSubsequenceLen(list);console.log("Length of longest increasing subsequence is :",longestLen);// 4

Shortest Unsorted Continuous Subarray Length

Method to find the length of the shortest unsorted continuous subarray from an input array.

import{getShortestUnsortedSubarray}from'structures-wiz';constlist=[2,6,4,8,10,9,15];constshortedSubArrayLen=getShortestUnsortedSubarray(list);console.log("Length of hortest Unsorted Continuous Subarray is :",shortedSubArrayLen);// 5

Is Cycle Present in Graph

Method to find the cycle is to use Topological sort. First we find the inDegree of each node and then add thosewhich are not dependent on any other node to the queue and traverse through the queue until all of each childs aretraversed, meanwhile reducing the inDegree and adding to the queue when inDegree is 0.

Param1: Number of nodesParam2: List of edges in an array where each edge is in the format [outEdge, inEdge]

import{isCyclePresentInGraph}from'structures-wiz';constedges=[[1,0]];//Way 1 - Debugger OFFconstisCyclePresent=isCyclePresentInGraph(2,edges);//Way 1 - Debugger ONconstisCyclePresentDebugON=isCyclePresentInGraph(2,edges,true);console.log("Is Cycle Present :",isCyclePresent);// false

Diameter of a Binary Tree

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 OFFconstdiameter=getDiameterOfBinaryTree(root);//Binary Tree - [1,2,3,4,5]//Debugging ONconstdiameterDebugON=getDiameterOfBinaryTree(root,true);//Binary Tree - [1,2,3,4,5]console.log("Diameter is :",diameter);// 3

Is Sorted List

Function to check if the list is sorted or not in an ascending order.

import{isSortedList}from'structures-wiz';constlist=[20,3,1,5,7,2,2,6,99,0];//Ascending Order - (default)constisSorted=isSortedList(list);//Descending Order - pass a comparator as second argumentconstisSorted=isSortedList(list,(a,b)=>b-a);//Debugger ON (3rd Argument)constisSorted=isSortedList(list,(a,b)=>b-a,true);console.log("Is List Sorted:",isSorted);// false

Patience Sort

Patience sort uses series of increasing stacks like the game of Solitaire to sort a listin 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';constlist=[20,3,1,5,7,2,2,6,99,0];//Debugging OFFconstsortedList=getPatienceSortedList(list);console.log("Sorted List is :",sortedList);// [ 0, 1, 2,  2,  3, 5, 6, 7, 20, 99]//Debugging ONconstsortedDebugList=getPatienceSortedList(list,true);

Merge Sort

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';constlist=[20,3,1,5,7,2,2,6,99,0];//Debugging OFFconstsortedList=getMergeSortedList(list);//Debugging ONconstsortedDebugList=getMergeSortedList(list,true);console.log("Sorted List is :",sortedList);// [ 0, 1, 2,  2,  3, 5, 6, 7, 20, 99]

Topologically Sort in a Graph

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]] - FORMATconstedges=[[1,0],[2,0],[3,1],[3,2]];//DEBUGGER OFFconsttopologicallySortedList=getTopologicallySortedGraph(4,edges);//DEBUGGER ONconsttopologicallySortedListDebug=getTopologicallySortedGraph(4,edges,true);console.log("Topologically Sorted List is :",topologicallySortedList);// [0,1,2,3]

Is Increasing Triplet Present

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';constlist=[1,2,3,4,5];constisIncTripletPresent=isIncreasingTripletSubsequencePresent(list);console.log("Is Increasing Triplet present :",isIncTripletPresent);// true

Number of Balanced Binary Trees possible of Height h

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';consth=2;constmaxBBT=getNumberOfBalancedBinaryTrees(h);console.log("Max Number of BBT from h:",maxBBT);// 3

Number of Unique Binary Trees with 'n' unique nodes

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';constn=3;constuniqBT=getUniqueBinaryTreesNum(n);console.log("Number of Unique Binary Trees:",uniqBT);// 5

Fenwick Tree (Binary Indexed Tree)

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.

Instantiation

import{FenwickTree}from'structures-wiz';constfTree=newFenwickTree(5);

Methods

Following are the methods exposed for usage:

getTree()

Gets the Fenwick Tree for corresponding input list.

import{FenwickTree}from'structures-wiz';constfTree=newFenwickTree(5);console.log("Tree:",fTree.getTree())

add(index, value)

Adds value to the index of the original array.

import{FenwickTree}from'structures-wiz';letfTree=newFenwickTree(5);fTree.add(0,1);console.log("Tree:",fTree.getTree());

getSum(index)

Gets the Sum till the index the created Fenwick Tree.

import{FenwickTree}from'structures-wiz';letfTree=newFenwickTree(5);fTree.add(1,1);fTree.add(3,1);console.log(fTree.getSum(4));console.log("Sum till 4th index:",fTree.getSum(4));

Segment Tree

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

Instantiation

import{SegmentTree}from'structures-wiz';constsegTree=newSegmentTree([5,2,1,3,4,6,7,9,8,3]);

Methods

Following are the methods exposed for usage:

getTree()

Gets the Segment Tree for corresponding input list.

import{SegmentTree}from'structures-wiz';constsegTree=newSegmentTree([5,2,1,3,4,6,7,9,8,3]);console.log("Tree:",segTree.getTree())

getList()

Gets the current state of original array of Segment Tree.

import{SegmentTree}from'structures-wiz';constsegTree=newSegmentTree([5,2,1,3,4,6,7,9,8,3]);console.log("List:",segTree.getList())

getRangeSum()

Gets the Sum of a range from the created Segment Tree.

import{SegmentTree}from'structures-wiz';constsegTree=newSegmentTree([5,2,1,3,4,6,7,9,8,3]);//Parameters are lower and upper bound indexconsole.log("Sum from 0th index to 2nd index:",segTree.getRangeSum(0,2))

Priority Queue

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.

Instantiation

import{PriorityQueue}from'structures-wiz';//Default contructor will create a Max-HeapconstpriorityQ=newPriorityQueue();//Passing custom comparator for Min HeapconstpriorityQ=newPriorityQueue((a,b)=>a<=b);

Methods

Following are the methods exposed for usage:

enqueue(value, priority)

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-HeapconstpriorityQ=newPriorityQueue();//Way 1 -  Without specifying Priority (Priority = Value in this case by default)priorityQ.enqueue(10);//Way 2 -  Passing Value & PrioritypriorityQ.enqueue(10,100);

dequeue()

Removes the Maximum element (In case of Max-Heap config) or Minimum Element (In case of Min-Heap config).

import{PriorityQueue}from'structures-wiz';constpriorityQ=newPriorityQueue();priorityQ.enqueue(10,100);priorityQ.enqueue(20,90);priorityQ.dequeue();//Removes the Highest Priority Element

getSize()

Returns the size of the queue.

import{PriorityQueue}from'structures-wiz';constpriorityQ=newPriorityQueue();priorityQ.enqueue(10,100);priorityQ.enqueue(20,90);priorityQ.getSize();//2

getLeftIndex( index )

Returns the left child index of the passed index.

import{PriorityQueue}from'structures-wiz';constpriorityQ=newPriorityQueue();priorityQ.enqueue(10,100);priorityQ.enqueue(20,80);priorityQ.enqueue(60,90);priorityQ.enqueue(40,20);priorityQ.enqueue(70,200);priorityQ.enqueue(50,40);constleftIndex=priorityQ.getLeftIndex(0);console.log("LeftIndex of 0th index is :",leftIndex);//1

getRightIndex( index )

Returns the right child index of the passed index.

import{PriorityQueue}from'structures-wiz';constpriorityQ=newPriorityQueue();priorityQ.enqueue(10,100);priorityQ.enqueue(20,80);priorityQ.enqueue(60,90);priorityQ.enqueue(40,20);priorityQ.enqueue(70,200);priorityQ.enqueue(50,40);constrightIndex=priorityQ.getRightIndex(0);console.log("RightIndex of 0th index is :",rightIndex);//2

getParentIndex( index )

Returns the parent node index of the passed index.

import{PriorityQueue}from'structures-wiz';constpriorityQ=newPriorityQueue();priorityQ.enqueue(10,100);priorityQ.enqueue(20,80);priorityQ.enqueue(60,90);priorityQ.enqueue(40,20);priorityQ.enqueue(70,200);priorityQ.enqueue(50,40);constparentIndex=priorityQ.getParentIndex(1);console.log("ParentIndex of 1st index is :",parentIndex);//0

peek()

Returns the Maximum element (In case of Max-Heap config) or Minimum Element (In case of Min-Heap config).

import{PriorityQueue}from'structures-wiz';constpriorityQ=newPriorityQueue();priorityQ.enqueue(10,100);priorityQ.enqueue(20,80);priorityQ.enqueue(60,90);priorityQ.enqueue(40,20);priorityQ.enqueue(70,200);priorityQ.enqueue(50,40);constpeekValue=priorityQ.peek();console.log("Peak Value is :",peekValue);//[ 70 , 200 ]

getHeap()

Returns the current heap

import{PriorityQueue}from'structures-wiz';constpriorityQ=newPriorityQueue();priorityQ.enqueue(10,100);priorityQ.enqueue(20,80);priorityQ.enqueue(60,90);priorityQ.enqueue(40,20);priorityQ.enqueue(70,200);priorityQ.enqueue(50,40);constHeap=priorityQ.getHeap();console.log("Heap is :",Heap);/*[  [ 70, 200 ],  [ 10, 100 ],  [ 60, 90 ],  [ 40, 20 ],  [ 20, 80 ],  [ 50, 40 ]]*/

getSortedHeap()

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';constpriorityQ=newPriorityQueue();priorityQ.enqueue(10,100);priorityQ.enqueue(20,80);priorityQ.enqueue(60,90);priorityQ.enqueue(40,20);priorityQ.enqueue(70,200);priorityQ.enqueue(50,40);constsortedHeap=priorityQ.getSortedHeap();console.log("Sorted Heap is :",sortedHeap);/*[  [ 70, 200 ],  [ 10, 100 ],  [ 60, 90 ],  [ 20, 80 ],  [ 50, 40 ],  [ 40, 20 ]]*/

getKth(k)

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';constpriorityQ=newPriorityQueue();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 )constk=2;constkthElement=priorityQ.getKth(2);console.log("kth - ",k," is :",kthElement);//[ 10 , 100 ]

getHeight()

Returns the height of the heap.

import{PriorityQueue}from'structures-wiz';constpriorityQ=newPriorityQueue();priorityQ.enqueue(10,100);priorityQ.enqueue(20,80);priorityQ.enqueue(60,90);priorityQ.enqueue(40,20);priorityQ.enqueue(70,200);priorityQ.enqueue(50,40);constheight=priorityQ.getHeight();console.log("Height of Heap is :",height);// 2

isLeafNode(index)

Returns true if the node at the index is a leaf node else returns false.

import{PriorityQueue}from'structures-wiz';constpriorityQ=newPriorityQueue();priorityQ.enqueue(10,100);priorityQ.enqueue(20,80);priorityQ.enqueue(60,90);priorityQ.enqueue(40,20);priorityQ.enqueue(70,200);priorityQ.enqueue(50,40);constindex=4;constisIndexLeafNode=priorityQ.isLeafNode(index);console.log("Is ",index,"Th a leaf node ?",isIndexLeafNode);// true

getLeafNodes()

Returns the list of leaf nodes.

import{PriorityQueue}from'structures-wiz';constpriorityQ=newPriorityQueue();priorityQ.enqueue(10,100);priorityQ.enqueue(20,80);priorityQ.enqueue(60,90);priorityQ.enqueue(40,20);priorityQ.enqueue(70,200);priorityQ.enqueue(50,40);constleafNodes=priorityQ.getLeafNodes();console.log("Leaf Nodes are",leafNodes);// [ [ 60, 90 ], [ 40, 20 ], [ 20, 80 ], [ 50, 40 ] ]

print()

Prints the current state of the queue.

import{PriorityQueue}from'structures-wiz';constpriorityQ=newPriorityQueue();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 ]]*/

printSortedHeap()

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';constpriorityQ=newPriorityQueue();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 ]]*/

clear()

Clears the queue.

import{PriorityQueue}from'structures-wiz';constpriorityQ=newPriorityQueue();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();// []

Stack

Implementation of stack is pretty straightforward with a few additional features like creating a stack with unique elements and removing duplicates in a stack.

Instantiation

import{Stack}from'structures-wiz';//Default contructor will create a Stack with empty elements and "isUnique" flag falseconststack=newStack();//Passing custom values and setting the second parameter makes the Stack only accept unique ElementsconstuniqueStack=newStack([20,90],true);

Methods

Following are the methods exposed for usage:

getSize()

import{Stack}from'structures-wiz';conststack=newStack([20,80,10,90]);constsize=stack.getSize();console.log("Size of the stack is:",size);// 4

push(value)

Pushes the received value at the end of the stack.

import{Stack}from'structures-wiz';conststack=newStack();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 ]

pushAll(values)

Pushes all the received values at the end of the stack.

import{Stack}from'structures-wiz';//Config 1 - Pass set of valuesconststack=newStack();stack.pushAll(100,100,400,600,100,800);stack.print()// [ 100, 100, 400, 600, 100, 800 ]//Config 2 - Pass Array of valuesconststack=newStack();stack.pushAll([100,100,400,600,100,800]);stack.print()// [ 100, 100, 400, 600, 100, 800 ]

peek()

Returns the top most value.

import{Stack}from'structures-wiz';conststack=newStack();stack.pushAll(100,100,400,600,100,800);constpeekValue=stack.peek();console.log("Peek Value is :",peekValue);//800

pop()

Removes and returns the top most element of the stack.

import{Stack}from'structures-wiz';conststack=newStack();stack.pushAll(100,100,400,600,100,800);stack.print();//[ 100, 100, 400, 600, 100, 800 ]constpoppedValue=stack.pop();console.log("Popped Value is :",poppedValue);//800stack.print();//[ 100, 100, 400, 600, 100 ]

removeDuplicates()

Removes the duplicates elements of the stack.

import{Stack}from'structures-wiz';conststack=newStack();stack.pushAll(100,100,400,600,100,800);stack.print();//[ 100, 100, 400, 600, 100, 800 ]stack.removeDuplicates();stack.print();//[ 100, 400, 600, 800 ]

clear()

Removes all the elements of the stack.

import{Stack}from'structures-wiz';conststack=newStack();stack.pushAll(100,100,400,600,100,800);stack.print();//[ 100, 100, 400, 600, 100, 800 ]stack.clear();stack.print();//[ ]

print()

Prints the current stack state.

import{Stack}from'structures-wiz';conststack=newStack();stack.pushAll(100,100,400,600,100,800);stack.print();//[ 100, 100, 400, 600, 100, 800 ]

LinkedList

Instantiation

import{LinkedList}from'structures-wiz';//Instantiates the LinkedList with no nodesconstlinkedList=newLinkedList();linkedList.print();//Linked List is empty

Methods

Following are the methods exposed for usage.

getSize()

import{LinkedList}from'structures-wiz';constlinkedList=newLinkedList();linkedList.addAtHead(2);linkedList.addAtHead(5);linkedList.addAtTail(15);linkedList.addAtHead(9);constsize=linkedList.getSize();console.log("Size of the LinkedList is:",size);//4

addAtHead(value)

Method to add a value at the start of the LinkedList.

import{LinkedList}from'structures-wiz';constlinkedList=newLinkedList();linkedList.addAtHead(2);linkedList.addAtHead(5);linkedList.print();// 5 -> 2 -> NULL

addAtTail(value)

Method to add a value at the end of the LinkedList.

import{LinkedList}from'structures-wiz';constlinkedList=newLinkedList();linkedList.addAtHead(2);linkedList.addAtHead(5);linkedList.addAtTail(15);linkedList.print();// 5 -> 2 -> 15 -> NULL

removeAtHead(value)

Method to remove the value at the start of the LinkedList.

import{LinkedList}from'structures-wiz';constlinkedList=newLinkedList();linkedList.addAtHead(2);linkedList.addAtHead(5);linkedList.print();// 5 -> 2 -> NULLlinkedList.removeAtHead(5);linkedList.print();// 2 -> NULL

removeAtTail(value)

Method to remove the value at the end of the LinkedList.

import{LinkedList}from'structures-wiz';constlinkedList=newLinkedList();linkedList.addAtHead(2);linkedList.addAtHead(5);linkedList.addAtTail(15);linkedList.print();// 5 -> 2 -> 15 -> NULLlinkedList.removeAtTail(5);linkedList.print();// 5 -> 2 -> NULL

print()

Prints the LinkedList.

import{LinkedList}from'structures-wiz';constlinkedList=newLinkedList();linkedList.print();// Linked List is emptylinkedList.addAtHead(2);linkedList.print();// 2 -> NULLlinkedList.addAtHead(5);linkedList.print();//5 -> 2 -> NULLlinkedList.addAtTail(15);linkedList.print();// 5 -> 2 -> 15 -> NULLlinkedList.addAtHead(9);linkedList.print();//9 -> 5 -> 2 -> 15 -> NULL

sort()

Sorts the LinkedList.

import{LinkedList}from'structures-wiz';constlinkedList=newLinkedList();linkedList.addAtHead(22);linkedList.addAtHead(5);linkedList.addAtTail(15);linkedList.print();//5 -> 22 -> 15 -> NULLlinkedList.sort();linkedList.print();//5 -> 15 -> 22 -> NULL

getHead()

Returns the head node of the LinkedList

import{LinkedList}from'structures-wiz';constlinkedList=newLinkedList();linkedList.addAtHead(22);linkedList.addAtHead(5);linkedList.addAtTail(15);linkedList.print();//5 -> 22 -> 15 -> NULLconstcurrentHead=linkedList.getHead();console.log("Head Value:",currentHead.val);//Head Value: 5

getTail()

Returns the head node of the LinkedList

import{LinkedList}from'structures-wiz';constlinkedList=newLinkedList();linkedList.addAtHead(22);linkedList.addAtHead(5);linkedList.addAtTail(15);linkedList.print();//5 -> 22 -> 15 -> NULLconstcurrentTail=linkedList.getTail();console.log("Tail Value:",currentHead.val);//Head Value: 15

getArray()

Returns the Linked list in array format

import{LinkedList}from'structures-wiz';constlinkedList=newLinkedList();linkedList.addAtHead(22);linkedList.addAtHead(5);linkedList.addAtTail(15);linkedList.print();//5 -> 22 -> 15 -> NULLconstarray=linkedList.getArray();console.log("List converted to Array:",array);//[5, 22, 15]

clear()

Clears the LinkedList

import{LinkedList}from'structures-wiz';constlinkedList=newLinkedList();linkedList.addAtHead(22);linkedList.addAtHead(5);linkedList.addAtTail(15);linkedList.print();//5 -> 22 -> 15 -> NULLlinkedList.clear();linkedList.print()//Linked List is empty

mapRunner()

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';functiondouble(node){node.val*=2;}constlinkedList=newLinkedList();linkedList.addAtHead(22);linkedList.addAtHead(5);linkedList.addAtTail(15);linkedList.print();//5 -> 22 -> 15 -> NULLlinkedList.mapRunner(double);linkedList.print()//10 -> 44 -> 30 -> NULL

Contribute

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.

License

MIT

About

An optimised 🚀 implementation of Data structures & Algorithms like Fenwick Trees, Segment Trees, Stacks, Priority Queues, Linked Lists etc for enterprise usage in our favourite ❤️ language - JavaScript

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors2

  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp