TypeScript Data Structures that you need!

Overview

TSDS

TypeScript Data Structures that you need!

NPM npm (scoped) npm bundle size (scoped)

Doc Website

Introduction

A data structure is a way to store and organize data in order to facilitate access and modifications. No single data structure works well for all purposes, and so it is important to know the strengths and limitations of several of them.

Example:

You may have used Map before, The Map object holds key-value pairs and remembers the original insertion order of the keys. Any value (both objects and primitive values) may be used as either a key or a value.

The Map is similar to Object But, The keys in Map are ordered in a simple, straightforward way: A Map object iterates entries, keys, and values in the order of entry insertion.

The Map is builtin in javascript but, There are lots of other useful Data Structures that are not implemented in JavaScript or TypeScript. We attempt to implement them in this library.

Table of Contents

Installation

To install and save in your package.jsondependencies, run:

npm install @samavati/tsds

APIs

LinkedList

A linked list is a data structure in which the objects are arranged in a linear order. Unlike an array, however, in which the linear order is determined by the array indices, the order in a linked list is determined by a pointer in each object.

Usage

import { LinkedList } from '@samavati/tsds';

// instantiate new linked list without initial set-up
const list = new LinkedList();

// instantiate new linked list with initial values
const list2 = new LinkedList([1, 2, 3, 4, 5]);

The list supports iterator protocols allowing it to be used with the for...of

import { LinkedList } from '@samavati/tsds';

const list = new LinkedList([1, 2, 3]);

for (let item of list) {
	console.log(item)
}
// 1
// 2
// 3

LinkedList.Properties

LinkedList.first

Definition

Gets the first node of the LinkedList.

Property Value

LinkedListNode

The first LinkedListNode of the LinkedList.

Example

const list = new LinkedList<number>([1, 2, 3, 4]);

list.first // => LinkListNode(1)

Remarks

If the LinkedList is empty, the First and Last properties contain null.

Retrieving the value of this property is an O(1) operation.

LinkedList.last

Definition

Gets the last node of the LinkedList.

Property Value

LinkedListNode

The last LinkedListNode of the LinkedList.

Example

const list = new LinkedList<number>([1, 2, 3, 4]);

list.last // => LinkListNode(4)

Remarks

If the LinkedList is empty, the First and Last properties contain null.

Retrieving the value of this property is an O(1) operation.

LinkedList.length

Definition

Gets the number of nodes actually contained in the LinkedList.

Property Value

number

Example

const list = new LinkedList<number>([1, 2, 3, 4]);

list.length // => 4

Remarks

Retrieving the value of this property is an O(1) operation.

LinkedList.Methods

LinkedList.append

Definition

Adds a new node or value at the end of the LinkedList.

Parameters

value T: The new value to add at the end of the LinkedList.

Example

const list = new LinkedList<number>([1, 2, 3, 4]);

list.length // => 4
list.append(5)
list.length // => 5
list.last // => LinkListNode(5)

Remarks

This method is an O(1) operation.

LinkedList.clear

Definition

Removes all nodes from the LinkedList.

Example

const list = new LinkedList<number>([1, 2, 3, 4]);

list.length // => 4
list.clear();
list.length // => 0

LinkedList.delete

Definition

Removes the first occurrence of a node or value from the LinkedList.

Example

const list = new LinkedList<number>([1, 2, 3, 4]);

list.length // => 4
list.delete(4)
list.length // => 3
list.last // => LinkListNode(3)

Remarks

This method is an O(n) operation.

LinkedList.deleteFirst

Definition

Removes the node at the start of the LinkedList.

Example

const list = new LinkedList<number>([1, 2, 3, 4]);

list.length // => 4
list.deleteFirst();
list.length // => 3
list.first // => LinkListNode(2)

Remarks

This method is an O(1) operation.

LinkedList.find

Definition

Finds the first node that contains the specified value.

Parameters

value T: The value to locate in the LinkedList.

Returns

The first LinkedListNode that contains the specified value, if found; otherwise, null.

Example

const list = new LinkedList<number>([1, 2, 3, 4]);

const item = list.find(2)

const nullItem = list.find(10) // => null

Remarks

This method is an O(n) operation.

LinkedList.get

Definition

Returns Node at the specified index

Parameters

index number: index of the Node starts, from 0

Returns

LinkedListNode of the specified index, if index is less than length; otherwise, null.

Example

const list = new LinkedList<number>([1, 2, 3, 4]);

const item = list.get(2)

const nullItem = list.get(10) // => null

Remarks

This method is an O(n) operation.

LinkedList.includes

Definition

Determines whether a value is in the LinkedList.

Parameters

value T: The value to locate in the LinkedList.

Returns

boolean

true if value is found in the LinkedList; otherwise, false.

Example

const list = new LinkedList<number>([1, 2, 3, 4]);

list.includes(2) // => true
list.includes(10) // => false

Remarks

This method is an O(n) operation.

LinkedList.insertAfter

Definition

Adds a new node or value after an existing node in the LinkedList.

Example

const list = new LinkedList<number>([1, 2, 3, 4]);

const item = list.get(2);
if (item) {
	list.insertAfter(item, 'hello');

	const world = new LinkedListNode('world');
	list.insertAfter(item, world);
}

Remarks

This method is an O(1) operation.

LinkedList.prepend

Definition

Adds a new node or value at the start of the LinkedList.

Parameters

value T: The new value to add at the start of the LinkedList.

Example

const list = new LinkedList<number>([1, 2, 3, 4]);

list.length // => 4
list.prepend(0)
list.length // => 5
list.first // => LinkListNode(0)

Remarks

This method is an O(1) operation.

LinkedList.toArray

Definition

Returns the entire LinkedList to a compatible one-dimensional Array

Example

const list = new LinkedList<number>([1, 2, 3, 4]);

list.prepend(0)
list.toArray() // => [0, 1, 2, 3, 4]

Remarks

This method is an O(n) operation.

Stack

A stack is an abstract data type that serves as a collection of elements, with two main principal operations: • Push, which adds an element to the collection, and • Pop, which removes the most recently added element that was not yet removed. The order in which elements come off a stack gives rise to its alternative name, LIFO (last in, first out). Additionally, a peek operation may give access to the top without modifying the stack.

Usage

import { Stack } from '@samavati/tsds';

// instantiate new Stack
const stack = new Stack();

The stack supports iterator protocols allowing it to be used with the for...of

import { Stack } from '@samavati/tsds';

const stack = new Stack();

stack.push(1);
stack.push(2);
stack.push(3);

for (const item of stack) {
	console.log(item)
}

// 3
// 2
// 1

Stack.Properties

Stack.length

Definition

Gets the number of elements contained in the Stack.

Property Value

number

Example

const stack = new Stack<number>();

stack.push(1);
stack.push(2);
stack.push(3);

stack.length // => 3

Remarks

Retrieving the value of this property is an O(1) operation.

Stack.Methods

Stack.clear

Definition

Removes all objects from the Stack.

Example

const stack = new Stack<number>();

stack.push(1);
stack.push(2);
stack.push(3);

stack.length // => 3
stack.clear()
stack.length // => 0

Stack.includes

Definition

Determines whether an element is in the Stack.

Parameters

**item T**The object to locate in the Stack.

Returns

boolean

true if item is found in the Stack; otherwise, false.

Example

const stack = new Stack<number>();

stack.push(1);
stack.push(2);
stack.push(3);

stack.includes(2) // => true
stack.includes(10) // => false

Remarks

This method is an O(n) operation.

Stack.peek

Definition

Returns the object at the top of the Stack without removing it.

Returns

T

The object at the top of the Stack.

Example

const stack = new Stack<number>();

stack.push(1);
stack.push(2);
stack.push(3);

stack.peek() // => 3

Remarks

This method is an O(1) operation.

Stack.pop

Definition

Removes and returns the object at the top of the Stack.

Returns

T

The object at the top of the Stack.

Example

const stack = new Stack<number>();

stack.push(1);
stack.push(2);
stack.push(3);

stack.pop() // => 3
stack.length // => 2

Remarks

This method is an O(1) operation.

Stack.push

Definition

Inserts an object at the top of the Stack.

Parameters

value T: The object to push onto the Stack.

Example

const stack = new Stack<number>();

stack.push(1);
stack.push(2);
stack.push(3);

stack.length // => 3

Remarks

This method is an O(1) operation.

Stack.toArray

Definition

Returns the entire Stack to a compatible one-dimensional Array

Example

const stack = new Stack<number>();

stack.push(1);
stack.push(2);
stack.push(3);

stack.toArray() // => [3, 2, 1]

Remarks

This method is an O(n) operation.

Queue

first-in-first-out (FIFO) data structure

A queue is a collection of entities that are maintained in a sequence and can be modified by the addition of entities at one end of the sequence and the removal of entities from the other end of the sequence. By convention, the end of the sequence at which elements are added is called the back, tail, or rear of the queue, and the end at which elements are removed is called the head or front of the queue

The operation of adding an element to the rear of the queue is known as enqueue, and the operation of removing an element from the front is known as dequeue

Usage

import { Queue } from  '@samavati/tsds';

// instantiate new Queue

const queue = new Queue();

The queue supports iterator protocols allowing it to be used with the for...of

import { Queue } from  '@samavati/tsds';

const queue = new Queue();

queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(3);

for (const  item  of  queue) {
console.log(item)
}
// 1
// 2
// 3

Queue.Properties

Queue.length

Definition

Gets the number of elements contained in the Queue.

Property Value

number

Example

const queue = new Queue<number>();

queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(3);

queue.length // => 3

Remarks

Retrieving the value of this property is an O(1) operation.

Queue.Methods

Queue.clear

Definition

Removes all objects from the Queue.

Example

const queue = new Queue<number>();

queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(3);

queue.length // => 3
queue.clear()
queue.length // => 0

Queue.includes

Definition

Determines whether an element is in the Queue.

Parameters

**item T**The object to locate in the Queue.

Returns

boolean

true if item is found in the Queue; otherwise, false.

Example

const queue = new Queue<number>();

queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(3);

queue.includes(2) // => true
queue.includes(10) // => false

Remarks

This method is an O(n) operation.

Queue.dequeue

Definition

Removes and returns the object at the beginning of the Queue

Returns

T

The object at the beginning of the Queue.

Example

const queue = new Queue<number>();

queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(3);

queue.dequeue() // => 3
queue.length // => 2

Remarks

This method is an O(1) operation.

Queue.enqueue

Definition

Inserts an object at the end of the Queue.

Parameters

value T: The object to add to the Queue.

Example

const queue = new Queue<number>();

queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(3);

queue.length // => 3

Remarks

This method is an O(1) operation.

Queue.peek

Definition

Returns the object at the beginning of the Stack without removing it.

Returns

T

The object at the beginning of the Queue.

Example

const queue = new Queue<number>();

queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(3);

queue.peek() // => 3

Remarks

This method is an O(1) operation.

Queue.toArray

Definition

Returns the entire Queue to a compatible one-dimensional Array

Example

const queue = new Queue<number>();

queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(3);

queue.toArray() // => [3, 2, 1]

Remarks

This method is an O(n) operation.

Built With

  • tsdx - Zero-config CLI for TypeScript package development

Contributing

Please do not hesitate to contact me and contribute on this project.

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

Authors

License

This project is licensed under the MIT License - see the LICENSE.md file for details

You might also like...

Data structures & algorithms implementations and coding problem solutions. Written in Typescript and tested with Jest. Coding problems are pulled from LeetCode and Daily Coding Problem.

technical-interview-prep Data structures & algorithms implementations and coding problem solutions. Written in Typescript and tested with Jest. Coding

Aug 5, 2022

A Typescript companion to the book A Common-Sense Guide to Data Structures and Algorithms by Jay Wengrow

A Typescript companion to the book A Common-Sense Guide to Data Structures and Algorithms by Jay Wengrow

This repository aims to be a companion to the book A Common-Sense Guide to Data Structures and Algorithms by Jay Wengrow. I rewrote most of the data s

Dec 3, 2022

Algorithms and Data Structures implemented in TypeScript for beginners, following best practices.

Algorithms and Data Structures implemented in TypeScript for beginners, following best practices.

The Algorithms - TypeScript TypeScript Repository of TheAlgorithms, which implements various algorithms and data structures in TypeScript. These imple

Dec 31, 2022

📝 Algorithms and data structures implemented in JavaScript with explanations and links to further readings

📝 Algorithms and data structures implemented in JavaScript with explanations and links to further readings

📝 Algorithms and data structures implemented in JavaScript with explanations and links to further readings

Dec 29, 2022

🥞Data Structures and Algorithms explained and implemented in JavaScript + eBook

🥞Data Structures and Algorithms explained and implemented in JavaScript + eBook

Data Structures and Algorithms in JavaScript This is the coding implementations of the DSA.js book and the repo for the NPM package. In this repositor

Jan 4, 2023

📝 Algorithms and data structures implemented in JavaScript with explanations and links to further readings

📝 Algorithms and data structures implemented in JavaScript with explanations and links to further readings

JavaScript Algorithms and Data Structures This repository contains JavaScript based examples of many popular algorithms and data structures. Each algo

Dec 31, 2022

ClojureScript's persistent data structures and supporting API from the comfort of vanilla JavaScript

mori A simple bridge to ClojureScript's persistent data structures and supporting APIs for vanilla JavaScript. Pull requests welcome. Breaking changes

Dec 31, 2022

Bringing an all Open-Source Platform to study Data Structures and Algorithms ⚡

NeoAlgo-Docs Bringing an all Open-Source Platform to study Data Structures and Algorithms ⚡ 🔧 Installation You will need to have NodeJS and Yarn inst

Jun 2, 2022

A collection of all the data structures implemented in javascript code

Data structures A collection of all the data structures implemented in javascript code, interview questions & problems ( with solutions ) Contributors

May 1, 2022

🛠️construct-js is a library for creating byte level data structures.

🛠️construct-js is a library for creating byte level data structures.

Dec 14, 2022

Code accompanying my illustrated Data structures video series on YouTube

Code accompanying my illustrated Data structures video series on YouTube

Dec 10, 2022

Serialization library for data-oriented design structures in JavaScript

Data-oriented Serialization for SoA/AoA A zero-dependency serialization library for data-oriented design structures like SoA (Structure of Arrays) and

Sep 27, 2022

Data Structures

Data Structures

Data-Structures Data structures implementaion using Javascript 🧑‍💻 Data-Structures is a Github repository that contains the implementaion of most of

Sep 14, 2022

The basics data structures implemented with javascript.

The basics data structures implemented with javascript.

Estrutura de Dados com Javascript Tudo que está escrito e codificado aqui dentro desse repositório foi retirado do livro Estrutura de dados e algoritm

Oct 18, 2022

easily building data structures that are serializable, validatable, and fully typed.

@davecode/structures [beta/wip] Structures is a TypeScript library for easily building data structure classes that are Serializable: Can convert rich

May 22, 2022

Ace your next Javascript coding interview by mastering data structures and algorithms.

The Coding Interview: Algorithms + Data Structures Ace your next Javascript coding interview by mastering data structures and algorithms. Problem 1: S

Sep 19, 2022

"To-do list" is a tool that helps to organize your day. It simply lists the things that you need to do and allows you to mark them as complete. You will build a simple website that allows for doing that, and you will do it using ES6 and Webpack!

To-do-list Description "To-do list" is a tool that helps to organize your day. It simply lists the things that you need to do and allows you to mark t

Oct 18, 2022

A superfast and easy to use knowledge base to help your customers get the info they need, when they need it most.

A superfast and easy to use knowledge base to help your customers get the info they need, when they need it most.

A superfast and easy to use knowledge base to help your customers get the info they need, when they need it most. helpkb is an open-source Next.js (A

Dec 5, 2022

Jetcap is a free online REST API that you can use whenever you need some fake data ✨

Jetcap Jetcap is a simple fake REST API for testing and prototyping. When to use ✨ Jetcap is a free online REST API that you can use whenever you need

Nov 13, 2022
Comments
  • Configure Mend Bolt for GitHub

    Configure Mend Bolt for GitHub

    Welcome to Mend Bolt for GitHub (formerly WhiteSource). This is an onboarding PR to help you understand and configure settings before Mend starts scanning your repository for security vulnerabilities.

    :vertical_traffic_light: Mend Bolt for GitHub will start scanning your repository only once you merge this Pull Request. To disable Mend Bolt for GitHub, simply close this Pull Request.


    What to Expect

    This PR contains a '.whitesource' configuration file which can be customized to your needs. If no changes were applied to this file, Mend Bolt for GitHub will use the default configuration.

    Before merging this PR, Make sure the Issues tab is enabled. Once you merge this PR, Mend Bolt for GitHub will scan your repository and create a GitHub Issue for every vulnerability detected in your repository.

    If you do not want a GitHub Issue to be created for each detected vulnerability, you can edit the '.whitesource' file and set the 'minSeverityLevel' parameter to 'NONE'.


    :question: Got questions? Check out Mend Bolt for GitHub docs. If you need any further assistance then you can also request help here.

    opened by mend-bolt-for-github[bot] 1
  • Manage to show @constructor in the doc

    Manage to show @constructor in the doc

    As we are generating the TSDS README.md file automatically, we need to show the @construtor section as "Creating a new ..." under summary section

    opened by samavati 0
Releases(v1.5.1)
Owner
Ehsan Samavati
Full-Stack Web Developer (mainly focused on Front-End)
Ehsan Samavati
Cache is easy to use data caching Node.js package. It supports Memcached, Redis, and In-Memory caching engines.

Cache Cache NPM implements wrapper over multiple caching engines - Memcached, Redis and In-memory (use with single threaded process in development mod

PLG Works 49 Oct 24, 2022
Template project for ComputerCraft programs written in TypeScript.

cc-tstl-template Template project for ComputerCraft programs written in TypeScript. Uses TypeScriptToLua to compile with ComputerCraft typing declarat

JackMacWindows 22 Dec 4, 2022
Opinionated, type-safe, zero-dependency max/min priority queue for JavaScript and TypeScript projects.

qewe qewe is an opinionated, type-safe, zero-dependency max/min priority queue for JavaScript and TypeScript projects. Installation Add qewe to your p

Jamie McElwain 2 Jan 10, 2022
Nanoservices in no time with seamless TypeScript support.

Nanolith Nanoservices in no time with seamless TypeScript support. Table of Contents About Defining a set of tasks Creating multiple sets of definitio

Matt Stephens 11 Dec 28, 2022
This project demonstrates how you can use the multi-model capabilities of Redis to create a real-time stock watchlist application.

Introduction This project demonstrates how you can use Redis Stack to create a real-time stock watchlist application. It uses several different featur

Redis Developer 43 Jan 2, 2023
Cloud Run Jobs Demos - A collection of samples to show you how and when to run a container to completion without a server

Cloud Run Jobs Demo Applications Cloud Run Jobs allows you to run a container to completion without a server. This repository contains a collection of

Google Cloud Platform 34 Dec 23, 2022
Out of the box modern User Interface, so you can see and manage your Workhorse jobs in realtime

WORKHORSE UI Out of the box modern User Interface, so you can see and manage your Workhorse jobs in realtime. Start local Run npm i Copy and name prox

Workhorse 2 Apr 15, 2022
Cloudflare Worker that will allow you to progressively migrate files from an S3-compatible object store to Cloudflare R2.

A Cloudflare Worker for Progressive S3 to R2 Blog Post: https://kian.org.uk/progressive-s3-to-cloudflare-r2-migration-using-workers/ This is a Cloudfl

Kian 29 Dec 30, 2022
A bot that notifies you on Slack whenever your company/product is mentioned on Hacker News. Powered by Vercel Functions & Upstash.

Hacker News Slack Bot A bot that notifies you on Slack whenever your company/product is mentioned on Hacker News. or deploy your own Built With Vercel

Vercel Labs 162 Jan 3, 2023
danfo.js is an open source, JavaScript library providing high performance, intuitive, and easy to use data structures for manipulating and processing structured data.

Danfojs: powerful javascript data analysis toolkit What is it? Danfo.js is a javascript package that provides fast, flexible, and expressive data stru

JSdata 4k Dec 29, 2022