npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

grant-linkedlist

v2.1.0

Published

A collection of Data Structures made with Typescript Generics

Downloads

19

Readme

Typescript Generics Data Structures

Author: Grant Ralls [email protected] https://www.grantralls.net

Foreword

I created this package for the value of practice. I wanted my own implementation of a Linked List using Typescript with Generics to use in future home-projects. Consider this a work in progress, not ready for production.


Compatibility

This is designed to be compatible with ES2017. This works best (at the time of writing) with Node 10+.


Example

Node 10.8.x

const LinkedList = require("linkedlist").LinkedList;

const newList = new LinkedList();

newList.append(3);
newList.contains(3); // true

Typescript

import { LinkedList } from 'linkedlist';

// number can be changed to another type.
const newList = new LinkedList<number>();

newList.append(4);
newList.contains(4); // true

API (Using Typescript)

Linked List

append(value: Type) returns void

The append function adds value to the end of the list.

Usage:

const newList = new LinkedList<number>();
newList.append(3);

prepend(value: Type) returns void

The prepend function adds value to the beginning of the list.

Usage:

const newList = new LinkedList<number>();
newList.prepend(3);

copy() returns LinkedList

The copy function deeply copies the list it is being run on.

Usage:

const newList = new LinkedList<number>();
const newCopy = newList.copy();

contains(value: type) returns boolean

The contains function searches iteratively for the first instance of value in the list.

Usage:

const newList = new LinkedList<number>();
newList.append(1);
newList.append(2);

newList.contains(2); // true
newList.contains(5); // false

getHeadValue() returns Type of List

The getHeadValue function returns the data of the head (far-left-end) node of the list. The return type is consistent with the type of the List.

Usage:

const newList = new LinkedList<number>();
newList.append(1);
newList.append(2);

newList.getHeadValue(); // 1

getTailValue() returns type of List

The getTailValue function returns the data of the tail (far-right-end) node of the list. The return type is consistent with the type of the List.

Usage:

const newList = new LinkedList<number>();
newList.append(1);
newList.append(2);

newList.getTailValue(); // 2

getSize() returns number

The size function returns the number of nodes in the list.

Usage:

const newList = new LinkedList<number>();
newList.append(1);
newList.append(2);

newList.getSize(); // 2

removeHead() returns void

The removeHead function removes the current head node from the list. The new head is determined as the next node from the previous head.

Usage:

const newList = new LinkedList<number>();
newList.append(1);
newList.append(2);
newList.removeHead();

newList.getHeadValue(); // 2

removeTail() returns void

The removeTail function removes the current tail node from the list. The new tail is determined as the previous node from the previous tail.
Note: The runtime for this function is O(n). This list is not doubly linked, therefore it needs to be fully iterated over to find the second to last node.

Usage:

const newList = new LinkedList<number>();
newList.append(1);
newList.append(2);
newList.removeTail();

newList.getTailValue(); // 1

removeAtIndex(indexOfNodeToRemove: number) returns void

The removeAtIndex function removes the node at the specified index. The two adjacent nodes are then chained together. Note: The runtime for this function is O(n). This list is not doubly linked, therefore it needs to be iterated over to find the node to remove.

Usage:

const newList = new LinkedList<number>();
newList.append(1);
newList.append(2);
newList.append(3);
newList.removeAtIndex(1);

// before removeAtIndex newList: 1 -> 2 -> 3
// after removeAtIndex newList: 1 -> 3

newList.getHeadValue(); // 1
newList.getTailValue(); // 3
newList.getSize(); // 2

getValueAtIndex(desiredIndex: number) returns type of List

The getValueAtIndex function returns the data of the node at the index specified. Note: The runtime for this function is O(n). This list is not doubly linked, therefore it needs to be iterated over to find the node to retrieve.

Usage:

const newList = new LinkedList<number>();
newList.append(1);
newList.append(2);
newList.append(3);

const result = newList.getValueAtIndex(1);

console.log(result); // 2

traverse() returns IterableIterator

The traverse function is a generator function that returns an Iterable Iterator. See Javascript Generator Docs for more info.

Usage:

const newList = new LinkedList<number>();
newList.append(1);
newList.append(2);

const listIterator = newList.traverse();

for (const value in listIterator) {
    console.log(value);
}

// Console: 1 2

Queue

push(value: Type) returns void

The push function adds value to the back of the queue.

Usage:

const newQueue = new Queue<number>();
newList.push(1);

pop(value: Type) returns T

The pop function removes the node at the front of the queue and returns the data that node was holding.

Usage:

const newQueue = new Queue<number>();
newList.push(1);

console.log(newList.pop()); // 1

front() returns T

The front function returns the data at the front of the queue.

Usage:

const newQueue = new Queue<number>();
newQueue.push(1);

console.log(newQueue.front()); // 1

back() returns T

The back function returns the data at the back of the queue.

Usage:

const newQueue = new Queue<number>();
newQueue.push(1);
newQueue.push(2);

console.log(newQueue.back()); // 2

size() returns T

The size function returns the number of nodes in the queue.

Usage:

const newQueue = new Queue<number>();
newQueue.push(1);
newQueue.push(2);
newQueue.push(2);

console.log(newQueue.size()); // 3

empty() returns boolean

The empty function returns true if the queue has no nodes associated with it.

Usage:

const newQueue = new Queue<number>();

console.log(newQueue.empty()); // true

newQueue.push(1);

console.log(newQueue.empty()); // false