0.0.8 • Published 7 years ago
ds-structures v0.0.8
Data Structues
data-structures and commonly used algorithms written in plain javascript, for eg Lists,stacks,queue etc.
Upcoming features Priority Queue, Trees,Trie(ds),Sorting Alogorithms,Searching Algorithms, Path Algorithms,Matrics...
Getting Started With List
How To Import for versions less than 0.0.7
const {List,Queue,Stack} = require('ds-structures');
importing in versions greater than 0.0.6
const {List,Stack,LinkList,Queue} from 'ds-structures/compiled';
List ExamplesCreating A List
const list = new List(1,2,3,4,'a','b','c') create a list of 1,2,3,4,a,b,c
Retreving A List
list.get(); //Returs the list;get element at particular index
list.get(index) // returns element at particular index;Add data to list
list.push(value) // pushes the value to your list and returns the listAdd data to the front of the list
list.pushFront(value) // adds the value at the 0 index of the listCheck element is in the list
list.contains(value) // returns true if value is present in the list or returns falseRemoving element at the last index of the list
list.popEnd(); // removes the last element of the list and returns the rest of the list;Removing element at the 0 index of the list
list.popFront() // removes the element at the 0 index and returns the rest of the list;Removing element from the list
list.remove(element) // this will remove the first occurance of element from the list and returns the rest of the list, element should be the value not the index;Removing element at particular index
list.splice(index) // removes the element at the given index and returns the rest of the listRemoving all the duplicates;
list.removeDuplicates(); // returns the unique list of elements Filtering from the list
filter based on mode -> two modes are there i.e (number and string ) mode
filter all the numbers from the list
list.filter('number') // filters all the numbers and returns the list without number , it may contain characters filter all the strings from the list
list.filter('string') // returns all the numbers;filter an element
list.filter(element) // returns the list without containing this elementfilter and list of elements
list.filter(new List('a',1,'b')) // returns the list without containing the new filter listpush element at particular index
list.pushAt(element,index) // pushes the given element at the given index , if index is not present pushFront is calledfill the array
list.fill(element,topIndex,bottomIndex) // fills the array with the given element from bottomIndex till topIndex, bottomIndex default is 0Getting Started With Stack
How To Import
const {Stack} = require('ds-structures');
Stack ExamplesCreate Instance Of Stack
const stack = new Stack(size_of_stack);
Push element to stack
stack.push(element) // returns the updated stack Pop element from stack
stack.pop(element) // returns the updated stackPeek
stack.peek() // returns peek element from the stackCheck OverFlow
stack.stackOverFlow() // returns true if the stack is full else falseCheck UnderFlow
stack.stackUnderFlow() // returns true if stack is empty else false;extend the size of stack
stack.extend(your_new_stack_size)Getting Started With Queue
const {Queue} = require('ds-structures');
const queue = new Queue();Pushing Element To Queue(enqueue)
queue.enqueue(item) // pushes item to the end of the queueRemoving Element From Queue(dequeue)
queue.dequeue(); // removes the first item from the queue;Get the front item from queue;
queue.first // returns the front element from queueGet the last item from queue
queue.last // returns the last element of the queueGet the queue size
queue.size // returns the size of queueLinked List ExamplesGetting Started With The LinkedList
const {LinkList} = require('ds-structure');
const myLinkList = new LinkList();
Set NextNode
myLinkList.setNext(10) // create the node having 10 as value and inserts in the linkListtraverse the linkList
myLinkList.traverse() // traverses the linkList and returns the list of linkList dataget the head node
myLinkList.getHead // returns the head node
myLinkList.getHead.getData() // data at the head node
myLinkList.getHead.setData(data) // sets the data at the head Nodeget the tail node
myLinkList.getTail // returns the tail of the linkList
myLinkList.getTail.getData() // returns the data at the tail node
myLinkList.getTail.setData(data) // sets the data at the tail nodeinsert the data at a particular node
myLinkList.insertAt(index,data) // inserts the data at the given index and pushes back the previous data at that nodedelete the node at the particular index
myLinkList.deleteAt(index) // removes the node at that index