linked-list-typescript
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.
yarnnpm 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 coverageyarn|npm run coverage:report
Usage
Importing:
;
API
LinkedList(...values: T[])
LinkedList()
Create an empty linked list by omitting any arguments during instantiation.
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.
;;
;;
Typescript will check if the values match the type given to the template when initializing the new list.
;; // 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.
;;; fooList.head.bar // => 1fooList.tail.bar // => 3val // => foo1
LinkedList(...values: any[])
Specify any
to allow the list to take values of any type.
list.length // => 3list.head // => 4list.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
:
;; for of list //4//5//6
...spread
:
;; manyArgs...list;//4//5//6
deconstruction
:
;; ;//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.
;;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.
;;list.tail // => 7
LinkedList#length :number
Query the length of the list. An empty list will return 0.
;;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.
;;list.length // => 4list.append8list.length // => 5list.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.
;;list.length // => 4list.length // => 4list.tail // => 7results // => 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.
;;list.length // => 4list.prepend3list.length // => 5list.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.
;;list.length // => 4list.length // => 4list.head // => 4result // => false
LinkedList#removeHead(): T
Removes the item at the head of the list and returns the item.
;;list.length // => 4list.length // => 3list.head // => 5val // => 4
LinkedList#removeTail(): T
Removes the item at the tail of the list and returns the item.
;;list.length // => 4list.length // => 3list.tail // => 6val // => 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.
;;list.length // => 4list.length // => 3list.tail // => 7val // => 6
;;list.length // => 4list.length // => 4list.tail // => 7val // => 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.
;;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.