A collection of common data structures written in TypeScript
npm install ts-data-structures
Creates a hash table of key and value pairs. Defaults to a size of 16 buckets. Each bucket represents a Singly Linked List of entries.
import { HashTable } from 'ts-data-structures'
// create a HashTable that takes keys of type string and values of type number
const table = new HashTable<string, number>();
// create a HashTable that has a size of 64 buckets
const biggerTable = new HashTable(64);
Get the number of buckets in the table.
table.size // 16
Get the number of entries in the table.
table.entries // 0
Add a new entry to the table. The table will be resized if it contains too many entries. If an entry with the given key already exists, it's value will be updated.
table.add('foo', 42)
table.add('bar', 8)
Find the value associated with a given key.
table.find('foo') // 42
table.find('zap') // undefined
Resize the table to a given number of buckets.
table.resize(32)
table.size // 32
Remove an entry from the table
table.entries // 2
table.remove('foo')
table.entries // 1
Creates a Singly Linked List of values.
import { SinglyLinkedList } from 'ts-data-structures'
// create a Singly Linked List values of type number
const list = new SinglyLinkedList<number>();
// create a Singly Linked List with three initial nodes
const populatedList = new SinglyLinkedList(1, 2, 3);
Add new entries to the singly linked list
list.add(1, 2, 3)
list.toArray() // [1, 2, 3]
Remove entries from the singly linked list
list.remove(2, 3)
list.toArray() // [1]