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-structures
Then where needed:
Typescript
list.addToLast1list.addToLast2
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
Methods
addToLast
public addToLastelement: T: void
Parameters
- element
T
- The element to be inserted
Description
Inserts an element in the ending of the Linked List.
addToHead
public addToHeadelement: T: void
Parameters
- element
T
- The element to be inserted
Description
Inserts an element in the beginning of the Linked List.
addElementAtPosition
public addElementAtPositionelement: 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 removeFirstElementFoundFromListelement: T: void
Parameters
- element
T
- The element to be removed
Description
Removes the first element found in the Linked List.
removeAllElementsFromList
public removeAllElementsFromListelement: 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 fromArrayelement: 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, returnsfalse
.
Description
Informs if the Linked List is empty.
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
Methods
add
public addelement: 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 removeFirstElementFoundFromListelement: T: void
Parameters
- element
T
- The element to be removed
Description
Removes the first element found in the Sorted List.
removeAllElementsFromList
public removeAllElementsFromListelement: T: void
Parameters
- element
T
- The element to be removed
Description
Removes all the elements found in the Sorted List.
fromArray
public fromArrayelement: 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, returnsfalse
.
Description
Informs if the Sorted List is empty.
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
Methods
insertToHead
public insertToHeadelement: T: void
Parameters
- element
T
- The element to be inserted
Description
Inserts an element in the beginning of the Doubly Linked List.
insertToTail
public insertToTailelement: T: void
Parameters
- element
T
- The element to be inserted
Description
Inserts an element in the ending of the Doubly Linked List.
insertAtPosition
public insertAtPositionelement: 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 deleteFirstFoundelement: T: void
Parameters
- element
T
- The element to be deleted
Description
Deletes the element
found in the Doubly Linked List.
deleteAllFound
public deleteAllFoundelement: 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 searchelement: 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 fromArrayelement: 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, 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
Methods
enqueue
public enqueueelement: 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
- number - Queue length.
Description
Gets the length of the Queue.
isEmpty
public isEmpty: boolean
Returns
- boolean - Returns
true
if 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
Methods
push
public pushelement: 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
- number - Stack length.
Description
Gets the length of the Stack.
isEmpty
public isEmpty: boolean
Returns
- boolean - Returns
true
if 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
Methods
insert
public insertkey: 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
Parameters
- key
string
- The key
Description
Removes an element from the Hash Table.
search
public searchkey: string: void
Parameters
- 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
Interfaces
INode
Methods
insert
public insertelement: T: void
Parameters
- element
T
- The element to be inserted
Description
Inserts an element in the Binary Search Tree.
delete
public delete
Parameters
- element
T
- The element to be removed
Description
Removes an element from the Binary Search Tree.
search
public searchelement: T: INode
Parameters
- 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: 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, returnsfalse
.
Description
Informs if the Binary Search Tree is empty.
fromArray
public fromArrayelement: T: void
Parameters
- elements
T[]
- Array of elements
Description
Inserts all the elements
in the Binary Search Tree.
License
MIT