sb-js-data-structures
TypeScript icon, indicating that this package has built-in type declarations

1.0.8 • Public • Published

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.

Linked List

Initializer

import { LinkedList } from 'sb-js-data-structures'
 
const list = new LinkedList()

Methods

addToLast
public addToLast(elementT)void
Parameters
  • element T - The element to be inserted
Description

Inserts an element in the ending of the Linked List.

addToHead
public addToHead(elementT)void
Parameters
  • element T - The element to be inserted
Description

Inserts an element in the beginning of the Linked List.

addElementAtPosition
public addElementAtPosition(elementT, positionnumber)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(elementT)void
Parameters
  • element T - The element to be removed
Description

Removes the first element found in the Linked List.

removeAllElementsFromList
public removeAllElementsFromList(elementT)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(elementT[])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.

Sorted List

Initializer

import { SortedList } from 'sb-js-data-structures'
 
const list = new SortedList()

Methods

add
public add(elementT)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(elementT)void
Parameters
  • element T - The element to be removed
Description

Removes the first element found in the Sorted List.

removeAllElementsFromList
public removeAllElementsFromList(elementT)void
Parameters
  • element T - The element to be removed
Description

Removes all the elements found in the Sorted List.

fromArray
public fromArray(elementT[])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.

Doubly Linked List

Initializer

import { DoublyLinkedList } from 'sb-js-data-structures'
 
const list = new DoublyLinkedList()

Methods

insertToHead
public insertToHead(elementT)void
Parameters
  • element T - The element to be inserted
Description

Inserts an element in the beginning of the Doubly Linked List.

insertToTail
public insertToTail(elementT)void
Parameters
  • element T - The element to be inserted
Description

Inserts an element in the ending of the Doubly Linked List.

insertAtPosition
public insertAtPosition(elementT, positionnumber)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(elementT)void
Parameters
  • element T - The element to be deleted
Description

Deletes the element found in the Doubly Linked List.

deleteAllFound
public deleteAllFound(elementT)void
Parameters
  • element T - The element to be deleted
Description

Deletes all the elements 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(elementT)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(elementT[])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.

Queue

Initializer

import { Queue } from 'sb-js-data-structures'
 
const queue = new Queue()

Methods

enqueue
public enqueue(elementT)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, 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.

Stack

Initializer

import { Stack } from 'sb-js-data-structures'
 
const stack = new Stack()

Methods

push
public push(elementT)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, 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.

Hash Table

Initializer

import { HashTable } from 'sb-js-data-structures'
 
const hash = new HashTable()

Methods

insert
public insert(keystring, valueT)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
  • key string - The key
Description

Removes an element from the Hash Table.

search
public search(keystring)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.

Binary Search Tree

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(elementT)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(elementT)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, returns false.
Description

Informs if the Binary Search Tree is empty.

fromArray
public fromArray(elementT[])void
Parameters
  • elements T[] - Array of elements
Description

Inserts all the elements in the Binary Search Tree.

License

MIT

Package Sidebar

Install

npm i sb-js-data-structures

Weekly Downloads

1

Version

1.0.8

License

MIT

Unpacked Size

606 kB

Total Files

38

Last publish

Collaborators

  • sambarros