1.1.3 • Published 3 years ago

single-ll v1.1.3

Weekly downloads
8
License
MIT
Repository
github
Last release
3 years ago

first-timers-only Commitizen friendly codecov

Single linked list implementation

A single linked list implementation which supports typescript.

Installation

npm install single-linked-list

Usage

import { SingleLinkedList } from 'single-linked-list';

const singleLinkedList1 = new SingleLinkedList<number>();
singleLinkedList.push(1);
singleLinkedList.push(2);
singleLinkedList.push(3);
singleLinkedList.push(4);
singleLinkedList.push(5);
console.log(singleLinkedList1.toArray()); // [1, 2, 3, 4, 5]

const singleLinkedList2 = new SingleLinkedList<number>();
singleLinkedList1.fromArray([11, 12, 13, 14, 15]);
console.log(singleLinkedList2.toArray()); // [11, 12, 13, 14, 15]

// It's also iterable
for (let key of singleLinkedList2) {
    console.log(singleLinkedList2[key]);
}
// Or
console.log([...singleLinkedList1]);

SingleLinkedList API

Attributes

attributesparametersdescription
headSingleLinkedListNodeThe first node in the list
tailSingleLinkedListNodeThe last node in the list
lengthnumberLength of the list

Functions

functionparametersreturn typedescription
pushvalue: TSingleLinkedListpush new node with value to the end of the list
unshiftvalue: TSingleLinkedListpush new node with value to the start of the list
popSingleLinkedListNode | undefinedremove the last node and return it
shiftSingleLinkedListNode | undefinedremove the first node and return it
getindex: numberSingleLinkedListNode | undefinedreturn node at certain index
removeindex: numberSingleLinkedListNode | undefinedremove node at certain index
setvalue: T, index: numberbooleanchange node value at certain index
insertvalue: T, index: numberbooleaninsert node at certain index
toArrayT[]return the linked list in the form of normal array
fromArrayarray: T[]SingleLinkedListCreate single linked list from normal array
reverseSingleLinkedListMutate the list by reversing it
clearvoidremove all nodes