Data Structures
JavaScript Data Structure written in TypeScript
Summary
Installation and Usage
Server-side:
Using npm:
npm install sb-ds-data-structures
Then 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): void
Parameters
- element
T
- The element to be inserted
Description
Inserts an element in the ending of the Linked List.
addToHead
public addToHead(element: T): void
Parameters
- 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): void
Parameters
- 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(): void
Description
Removes the element from the ending of the Linked List.
removeFromHead
public removeFromHead(): void
Description
Removes the element from the beginning of the Linked List.
removeFirstElementFoundFromList
public removeFirstElementFoundFromList(element: T): void
Parameters
- element
T
- The element to be removed
Description
Removes the first element found in the Linked List.
removeAllElementsFromList
public removeAllElementsFromList(element: T): void
Parameters
- element
T
- The element to be removed
Description
Removes all the elements found in the Linked List.
reverse
public reverse(): void
Description
Reverse the Linked List.
fromArray
public fromArray(element: T[]): void
Parameters
- 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(): number
Returns
- number - Linked List length.
Description
Gets the length of the Linked List.
isEmpty
public isEmpty(): boolean
Returns
- boolean - Returns
true
if the Linked List has no elements, otherwise, returns false
.
Description
Informs if the Linked List is empty.
print
public isEmpty(): boolean
Description
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): void
Parameters
- element
T
- The element to be inserted
Description
Inserts an element in the Sorted List.
removeFromLast
public removeFromLast(): void
Description
Removes the element from the ending of the Sorted List.
removeFromHead
public removeFromHead(): void
Description
Removes the element from the beginning of the Sorted List.
removeFirstElementFoundFromList
public removeFirstElementFoundFromList(element: T): void
Parameters
- element
T
- The element to be removed
Description
Removes the first element found in the Sorted List.
removeAllElementsFromList
public removeAllElementsFromList(element: T): void
Parameters
- element
T
- The element to be removed
Description
Removes all the elements found in the Sorted List.
fromArray
public fromArray(element: T[]): void
Parameters
- 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(): number
Returns
- number - Sorted List length.
Description
Gets the length of the Sorted List.
isEmpty
public isEmpty(): boolean
Returns
- boolean - Returns
true
if the Sorted List has no elements, otherwise, returns false
.
Description
Informs if the Sorted List is empty.
print
public isEmpty(): boolean
Doubly 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): void
Parameters
- element
T
- The element to be inserted
Description
Inserts an element in the beginning of the Doubly Linked List.
insertToTail
public insertToTail(element: T): void
Parameters
- 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): void
Parameters
- 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(): void
Description
Deletes an element in the beginning of the Doubly Linked List.
deleteFromTail
public deleteFromTail(): void
Description
Deletes an element in the ending of the Doubly Linked List.
deleteFirstFound
public deleteFirstFound(element: T): void
Parameters
- element
T
- The element to be deleted
Description
Deletes the element
found in the Doubly Linked List.
deleteAllFound
public deleteAllFound(element: T): void
Parameters
- element
T
- The element to be deleted
Description
Deletes all the element
s found in the Doubly Linked List.
displayForward
public displayForward(): void
Description
Displays all elements in the Doubly Linked List from the first to the last element.
displayBackward
public displayBackward(): void
Description
Displays all elements in the Doubly Linked List from the last to the first element.
search
public search(element: T): number
Parameters
- 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[]): void
Parameters
- elements
T[]
- Array of elements
Description
Inserts all the elements
at the ending of the Doubly Linked List.
getLength
public getLength(): number
Returns
- number - Doubly Linked List length.
Description
Gets the length of the Doubly Linked List.
isEmpty
public isEmpty(): boolean
Returns
- boolean - Returns
true
if the Doubly Linked List has no elements, otherwise, returns false
.
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): void
Parameters
- element
T
- The element to be inserted
Description
Inserts an element in the Queue.
dequeue
public dequeue(): void
Description
Removes an element from the Queue.
peek
public peek(): void
Description
Gets the element at the front of the Queue without removing it.
getLength
public getLength(): number
Returns
Description
Gets the length of the Queue.
isEmpty
public isEmpty(): boolean
Returns
- boolean - Returns
true
if the Queue has no elements, otherwise, returns false
.
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): void
Parameters
- element
T
- The element to be inserted
Description
Inserts an element in the Stack.
pop
public pop(): void
Description
Removes an element from the Stack.
peek
public peek(): void
Description
Gets the last element at the Stack without removing it.
getLength
public getLength(): number
Returns
Description
Gets the length of the Stack.
isEmpty
public isEmpty(): boolean
Returns
- boolean - Returns
true
if the Stack has no elements, otherwise, returns false
.
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): void
Parameters
- key
string
- The key - value
T
- The value to be inserted
Description
Inserts an element in the Hash Table.
delete
public delete(key: string): void
Parameters
Description
Removes an element from the Hash Table.
search
public search(key: string): void
Parameters
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): void
Parameters
- element
T
- The element to be inserted
Description
Inserts an element in the Binary Search Tree.
delete
public delete(element: T): void
Parameters
- element
T
- The element to be removed
Description
Removes an element from the Binary Search Tree.
search
public search(element: T): INode
Parameters
- element
T
- The element to be searched
Description
Gets the last element at the Binary Search Tree without removing it.
Returns
getLength
public getLength(): number
Returns
- number - Binary Search Tree length.
Description
Gets the number of elements of the Binary Search Tree.
isEmpty
public isEmpty(): boolean
Returns
- boolean - Returns
true
if the Binary Search Tree has no elements, otherwise, returns false
.
Description
Informs if the Binary Search Tree is empty.
fromArray
public fromArray(element: T[]): void
Parameters
- elements
T[]
- Array of elements
Description
Inserts all the elements
in the Binary Search Tree.
License
MIT