Skip to main content

Common data structures for use in algorithms and other manipulations

This package works with Node.js, Bun, Browsers
This package works with Node.js
This package works with Bun
This package works with Browsers
JSR Score
100%
Published
2 months ago (0.1.0)
class SinglyLinkedList
implements Iterable<T>

A singly linked list

Examples

Usage

const list = new SinglyLinkedList<number>();

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

console.log([...list]);
// Output: [1, 2, 3]

const array = [3, 8, 6];
const listFromArray = SinglyLinkedList.from(arr);
console.log([...listFromArray]);
// Output: [3, 8, 5]

Type Parameters

the type of values stored in linked list

Properties

readonly
head: T | null

The first node of singly linked list

readonly
isEmpty: boolean

Boolean value that indicates is list empty or not

readonly
size: number

The count of values stored in singly linked list

readonly
tail: T | null

The last node of singly linked list

Methods

[Symbol.iterator](): IterableIterator<T>
clear(): void

Clears the list

filter(predicate: Predicate<T>): SinglyLinkedList<T>

Create new list from filtered values

find(predicate: Predicate<T>): SinglyLinkedNode<T> | null

Finds a first value that satisfies condition predicate

get(index: number): SinglyLinkedNode<T> | null

A node with value that might be stored in the list by index

has(value: T): boolean

Boolean value that indicates is item stored in the singly linked list

insert(
index: number,
value: T,
): void

Inserts value to certain position in the list

map<U>(action: Action<T, U>): SinglyLinkedList<U>

Create new list from mapped values

pop(): T | null

Remove last element from the list

push(value: T): void

Adds value to the end of the singly linked list

remove(index: number): T | null

Remove element with certain index from the list

set(
index: number,
value: T,
): void

Change certain element value to provided

shift(): T | null

Remove first element from the list

unshift(value: T): void

Add value to the head of the list

Static Methods

from<U>(collection: ArrayLike<U> | Iterable<U>): SinglyLinkedList<U>

Creates new singly linked list from array like, iterable object

Add Package

npx jsr add @onetools/data-structures

Import symbol

import { SinglyLinkedList } from "@onetools/data-structures";

Add Package

yarn dlx jsr add @onetools/data-structures

Import symbol

import { SinglyLinkedList } from "@onetools/data-structures";

Add Package

pnpm dlx jsr add @onetools/data-structures

Import symbol

import { SinglyLinkedList } from "@onetools/data-structures";

Add Package

bunx jsr add @onetools/data-structures

Import symbol

import { SinglyLinkedList } from "@onetools/data-structures";