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

linked-list-typescript

v1.0.15

Published

simple typescript linked-list with generics typing

Downloads

48,390

Readme

linked-list-typescript

Build Status Coverage Status

Simple Typescript Linked List with generics type templating and support for iterator and iterable protocols.

See Also:

Installation

npm:

npm install --save linked-list-typescript

yarn:

yarn add linked-list-typescript

Building from source

install dev dependencies. There are no production dependencies.

yarn
npm install

build using the options in tsconfig.json

yarn|npm run build

run all package tests

yarn|npm run test

see the test coverage report

yarn|npm run coverage
yarn|npm run coverage:report

Usage

Importing:

import { LinkedList } from 'linked-list-typescript';
const { LinkedList } = require('linked-list-typescript')

API

LinkedList(...values: T[])

LinkedList()

Create an empty linked list by omitting any arguments during instantiation.

let list = new LinkedList<number>()

LinkedList(...values: T[])

Create a new list and initialize it with values. Values will be appended from left to right. i.e. the first argument will be at the head and the last argument will be at the tail.

Specify the type using the typescript templating to enable type-checking of all values going into and out of the list.

let items: number[] = [4, 5, 6, 7];
let list = new LinkedList<number>(...items);
let items: string[] = ['one', 'two', 'three', 'four'];
let list = new LinkedList<string>(...items);

Typescript will check if the values match the type given to the template when initializing the new list.

let items: = ['one', 'two', 'three', 4];
let list = new LinkedList<string>(...items); // arguments are not all strings

LinkedList(...values: Foo[])

Create a new list using custom types or classes. All values are retained as references and not copies so removed values can be compared using strict comparison.

class Foo {
  private val:number;
  constructor(val: number) {
    this.val = val;
  }
  get bar(): number { return this.val }
}

let foo1 = new Foo(1);
let foo2 = new Foo(2);
let foo3 = new Foo(3);

let fooList = new LinkedList<Foo>(foo1, foo2, foo3)

fooList.head.bar // => 1
fooList.tail.bar // => 3
let val = list.removeHead()
val // => foo1

LinkedList(...values: any[])

Specify any to allow the list to take values of any type.

let list = new LinkedList<any>(4, 'hello' { hello: 'world' })
list.length // => 3
list.head // => 4
list.tail // => { hello: 'world' }

LinkedList#[Symbol.iterator]

The list supports both iterator and iterable protocols allowing it to be used with the for...of and ...spread operators and with deconstruction.

for...of:

let items: number[] = [4, 5, 6];
let list = new LinkedList<number>(...items);

for (let item of list) {
  console.log(item)
}
//4
//5
//6

...spread:

let items: number[] = [4, 5, 6];
let list = new LinkedList<number>(...items);

function manyArgs(...args) {
  for (let i in args) {
    console.log(args[i])
  }
}
manyArgs(...list);
//4
//5
//6

deconstruction:

let items: number[] = [4, 5, 6, 7];
let list = new LinkedList<number>(...items);

let [a, b, c] = list;
//a => 4
//b => 5
//c => 6

LinkedList#head :T

Peek at the value at the head of the list. This will not remove the value from the list.

let items: number[] = [4, 5, 6, 7];
let list = new LinkedList<number>(...items);
list.head // => 4

LinkedList#tail :T

Peek at the value at the tail of the list. This will not remove the value from the list.

let items: number[] = [4, 5, 6, 7];
let list = new LinkedList<number>(...items);
list.tail // => 7

LinkedList#length :number

Query the length of the list. An empty list will return 0.

let items: number[] = [4, 5, 6, 7];
let list = new LinkedList<number>(...items);
list.length // => 4

LinkedList#append(val: T, checkDuplicates: boolean = false): boolean

Append an item to the end of the list. The new item will replace the previous tail item and subsequent calls to LinkedList#head will now recall the new item.

let items: number[] = [4, 5, 6, 7];
let list = new LinkedList<number>(...items);
list.length // => 4
list.append(8)
list.length // => 5
list.tail // => 8

The optional argument checkDuplicates is false by default. If set to true, it will check if the new value is already contained in the list. If the value is found to be a duplicate it will not be added and the method will return false.

Values are checked using strict === comparison. Checking for duplicates inserts the list into a Set and then checks if the value is contained in the set.

let items: number[] = [4, 5, 6, 7];
let list = new LinkedList<number>(...items);
list.length // => 4
let result = list.append(5, true)
list.length // => 4
list.tail // => 7
results // => false

LinkedList#prepend(val: T, checkDuplicates: boolean = false): boolean

Prepend an item to the beginning of the list. The new item will replace the previous head item and subsequent calls to LinkedList<T>#head will now recall the new item.

let items: number[] = [4, 5, 6, 7];
let list = new LinkedList<number>(...items);
list.length // => 4
list.prepend(3)
list.length // => 5
list.head // => 3

The optional argument checkDuplicates is false by default. If set to true, it will check if the new value is already contained in the list. If the value is found to be a duplicate it will not be added and the method will return false.

Values are checked using strict === comparison. Checking for duplicates inserts the list into a Set and then checks if the value is contained in the set.

let items: number[] = [4, 5, 6, 7];
let list = new LinkedList<number>(...items);
list.length // => 4
let result = list.prepend(4, true)
list.length // => 4
list.head // => 4
result // => false

LinkedList#removeHead(): T

Removes the item at the head of the list and returns the item.

let items: number[] = [4, 5, 6, 7];
let list = new LinkedList<number>(...items);
list.length // => 4
let val = list.removeHead()
list.length // => 3
list.head // => 5
val // => 4

LinkedList#removeTail(): T

Removes the item at the tail of the list and returns the item.

let items: number[] = [4, 5, 6, 7];
let list = new LinkedList<number>(...items);
list.length // => 4
let val = list.removeTail()
list.length // => 3
list.tail // => 6
val // => 7

LinkedList#remove(val: T): T

Removes the specified item from the list and returns the item for convenience. If the item can not be located in the list the method wil return undefined and the list will not be altered.

let items: number[] = [4, 5, 6, 7];
let list = new LinkedList<number>(...items);
list.length // => 4
let val = list.remove(6)
list.length // => 3
list.tail // => 7
val // => 6
let items: number[] = [4, 5, 6, 7];
let list = new LinkedList<number>(...items);
list.length // => 4
let val = list.remove(8)
list.length // => 4
list.tail // => 7
val // => undefined

LinkedList#toArray(): T[]

This method simply returns [...this].

Converts the list into an array and returns the array representation. This method does not mutate the list in any way.

Objects are not copied, so all non-primitive items in the array are still referencing the list items.

let items: number[] = [4, 5, 6, 7];
let list = new LinkedList<number>(...items);
let result = list.toArray()
result // => [4, 5, 6, 7]

Attribution

This linked-list was originally shared by Christos Monogios via his blog. The original code has been modified and extended to support typedef generics to allow for type checking on stored values for linked lists and iterable and iterator protocols.

License

MIT © Michael Sutherland