sb-js-data-structures
v1.0.8
Published
JavaScript Data Structure written in TypeScript
Maintainers
Readme
Data Structures
JavaScript Data Structure written in TypeScript
Summary
- Summary
- Installation and Usage
- Documentation
- License
Installation and Usage
Server-side:
Using npm:
npm install sb-ds-data-structuresThen where needed:
Typescript
import { LinkedList } from 'sb-js-data-structures'
const list = new LinkedList()
list.addToLast(1)
list.addToLast(2)Documentation
Linked List
Creates a Linked List, a data structure where each element is a separate object and the elements are linked using pointers.
Initializer
import { LinkedList } from 'sb-js-data-structures'
const list = new LinkedList()Methods
addToLast
public addToLast(element: T): voidParameters
- element
T- The element to be inserted
Description
Inserts an element in the ending of the Linked List.
addToHead
public addToHead(element: T): voidParameters
- element
T- The element to be inserted
Description
Inserts an element in the beginning of the Linked List.
addElementAtPosition
public addElementAtPosition(element: T, position: number): voidParameters
- element
T- The element to be inserted - position
number- position where the element should be inserted
Description
Inserts an element in a specific position of the Linked List, since position is less then the number of elements of the Linked List.
removeFromLast
public removeFromLast(): voidDescription
Removes the element from the ending of the Linked List.
removeFromHead
public removeFromHead(): voidDescription
Removes the element from the beginning of the Linked List.
removeFirstElementFoundFromList
public removeFirstElementFoundFromList(element: T): voidParameters
- element
T- The element to be removed
Description
Removes the first element found in the Linked List.
removeAllElementsFromList
public removeAllElementsFromList(element: T): voidParameters
- element
T- The element to be removed
Description
Removes all the elements found in the Linked List.
reverse
public reverse(): voidDescription
Reverse the Linked List.
fromArray
public fromArray(element: T[]): voidParameters
- elements
T[]- Array of elements
Description
Inserts all the elements at the ending of the Linked List.
toArray
public toArray(): T[]Description
Creates an array with all the elements of the Linked List.
getLength
public getLength(): numberReturns
- number - Linked List length.
Description
Gets the length of the Linked List.
isEmpty
public isEmpty(): booleanReturns
- boolean - Returns
trueif the Linked List has no elements, otherwise, returnsfalse.
Description
Informs if the Linked List is empty.
public isEmpty(): booleanDescription
Display the Linked List elements.
Sorted List
Creates a Sorted List, a data structure where each element is a separate object, the elements linked using pointers and the elements are sorted.
Initializer
import { SortedList } from 'sb-js-data-structures'
const list = new SortedList()Methods
add
public add(element: T): voidParameters
- element
T- The element to be inserted
Description
Inserts an element in the Sorted List.
removeFromLast
public removeFromLast(): voidDescription
Removes the element from the ending of the Sorted List.
removeFromHead
public removeFromHead(): voidDescription
Removes the element from the beginning of the Sorted List.
removeFirstElementFoundFromList
public removeFirstElementFoundFromList(element: T): voidParameters
- element
T- The element to be removed
Description
Removes the first element found in the Sorted List.
removeAllElementsFromList
public removeAllElementsFromList(element: T): voidParameters
- element
T- The element to be removed
Description
Removes all the elements found in the Sorted List.
fromArray
public fromArray(element: T[]): voidParameters
- elements
T[]- Array of elements
Description
Inserts all the elements at the ending of the Sorted List.
toArray
public toArray(): T[]Description
Creates an array with all the elements of the Sorted List.
getLength
public getLength(): numberReturns
- number - Sorted List length.
Description
Gets the length of the Sorted List.
isEmpty
public isEmpty(): booleanReturns
- boolean - Returns
trueif the Sorted List has no elements, otherwise, returnsfalse.
Description
Informs if the Sorted List is empty.
public isEmpty(): booleanDoubly Linked List
Creates a Doubly Linked List, a data structure where each element is a separate object and the elements linked using pointers to the next and the previous node.
Initializer
import { DoublyLinkedList } from 'sb-js-data-structures'
const list = new DoublyLinkedList()Methods
insertToHead
public insertToHead(element: T): voidParameters
- element
T- The element to be inserted
Description
Inserts an element in the beginning of the Doubly Linked List.
insertToTail
public insertToTail(element: T): voidParameters
- element
T- The element to be inserted
Description
Inserts an element in the ending of the Doubly Linked List.
insertAtPosition
public insertAtPosition(element: T, position: number): voidParameters
- element
T- The element to be inserted - position
number- position where the element should be inserted
Description
Inserts an element in a specific position of the Doubly Linked List, since position is less than the number of elements of the Doubly Linked List.
deleteFromHead
public deleteFromHead(): voidDescription
Deletes an element in the beginning of the Doubly Linked List.
deleteFromTail
public deleteFromTail(): voidDescription
Deletes an element in the ending of the Doubly Linked List.
deleteFirstFound
public deleteFirstFound(element: T): voidParameters
- element
T- The element to be deleted
Description
Deletes the element found in the Doubly Linked List.
deleteAllFound
public deleteAllFound(element: T): voidParameters
- element
T- The element to be deleted
Description
Deletes all the elements found in the Doubly Linked List.
displayForward
public displayForward(): voidDescription
Displays all elements in the Doubly Linked List from the first to the last element.
displayBackward
public displayBackward(): voidDescription
Displays all elements in the Doubly Linked List from the last to the first element.
search
public search(element: T): numberParameters
- element
T- The element to be searched
Returns
- number - returns the position found or -1 if the element doesn't exist on the list.
Description
Searches the element in the Doubly Linked List.
fromArray
public fromArray(element: T[]): voidParameters
- elements
T[]- Array of elements
Description
Inserts all the elements at the ending of the Doubly Linked List.
getLength
public getLength(): numberReturns
- number - Doubly Linked List length.
Description
Gets the length of the Doubly Linked List.
isEmpty
public isEmpty(): booleanReturns
- boolean - Returns
trueif the Doubly Linked List has no elements, otherwise, returnsfalse.
Description
Informs if the Doubly Linked List is empty.
Queue
Creates a Queue, a first-in-first-out (FIFO) data structure. In a FIFO data structure, the first element added to the queue will be the first one to be removed.
Initializer
import { Queue } from 'sb-js-data-structures'
const queue = new Queue()Methods
enqueue
public enqueue(element: T): voidParameters
- element
T- The element to be inserted
Description
Inserts an element in the Queue.
dequeue
public dequeue(): voidDescription
Removes an element from the Queue.
peek
public peek(): voidDescription
Gets the element at the front of the Queue without removing it.
getLength
public getLength(): numberReturns
- number - Queue length.
Description
Gets the length of the Queue.
isEmpty
public isEmpty(): booleanReturns
- boolean - Returns
trueif the Queue has no elements, otherwise, returnsfalse.
Description
Informs if the Queue is empty.
Stack
Creates a Stack, a last-in-first-out (LIFO) data structure. In a LIFO data structure, the last element added to the stack will be the first one to be removed.
Initializer
import { Stack } from 'sb-js-data-structures'
const stack = new Stack()Methods
push
public push(element: T): voidParameters
- element
T- The element to be inserted
Description
Inserts an element in the Stack.
pop
public pop(): voidDescription
Removes an element from the Stack.
peek
public peek(): voidDescription
Gets the last element at the Stack without removing it.
getLength
public getLength(): numberReturns
- number - Stack length.
Description
Gets the length of the Stack.
isEmpty
public isEmpty(): booleanReturns
- boolean - Returns
trueif the Stack has no elements, otherwise, returnsfalse.
Description
Informs if the Stack is empty.
Hash Table
Creates a Hash Table, a data structure that stores in an array format, where each data value has its own unique index value.
Initializer
import { HashTable } from 'sb-js-data-structures'
const hash = new HashTable()Methods
insert
public insert(key: string, value: T): voidParameters
- key
string- The key - value
T- The value to be inserted
Description
Inserts an element in the Hash Table.
delete
public delete(key: string): voidParameters
- key
string- The key
Description
Removes an element from the Hash Table.
search
public search(key: string): voidParameters
- key
string- The key
Description
Searches an element in a hash table.
Binary Search Tree
Creates a Binary Search Tree, a last-in-first-out (LIFO) data structure. In a LIFO data structure, the last element added to the tree will be the first one to be removed.
Creates a Binary Search Tree, a data structure that is a tree in which all the nodes follow: (1) The value of the key of the left sub-tree is less than the value of its parent (root) node's key; (2) The value of the key of the right sub-tree is greater than or equal to the value of its parent (root) node's key.
Initializer
import { BinarySearchTree } from 'sb-js-data-structures'
const tree = new BinarySearchTree()Interfaces
INode
export interface INode<T> {
element: T
right: INode<T> | null
left: INode<T> | null
}Methods
insert
public insert(element: T): voidParameters
- element
T- The element to be inserted
Description
Inserts an element in the Binary Search Tree.
delete
public delete(element: T): voidParameters
- element
T- The element to be removed
Description
Removes an element from the Binary Search Tree.
search
public search(element: T): INodeParameters
- element
T- The element to be searched
Description
Gets the last element at the Binary Search Tree without removing it.
Returns
- INode - The node found.
getLength
public getLength(): numberReturns
- number - Binary Search Tree length.
Description
Gets the number of elements of the Binary Search Tree.
isEmpty
public isEmpty(): booleanReturns
- boolean - Returns
trueif the Binary Search Tree has no elements, otherwise, returnsfalse.
Description
Informs if the Binary Search Tree is empty.
fromArray
public fromArray(element: T[]): voidParameters
- elements
T[]- Array of elements
Description
Inserts all the elements in the Binary Search Tree.
License
MIT
