Options
All
  • Public
  • Public/Protected
  • All
Menu

Class LinkedList<T>

A generic doubly-linked list. The namespace for the LinkedList class statics.

Type parameters

  • T

Hierarchy

  • LinkedList

Implements

  • IIterable<T>
  • IRetroable<T>

Index

Properties

Private _first

_first: LinkedListNode<T> | null = null

Private _last

_last: LinkedListNode<T> | null = null

Private _size

_size: number = 0

Accessors

first

  • get first(): T | undefined
  • The first value in the list.

    This is undefined if the list is empty.

    Complexity

    Constant.

    Returns T | undefined

firstNode

  • get firstNode(): INode<T> | null

isEmpty

  • get isEmpty(): boolean

last

  • get last(): T | undefined
  • The last value in the list.

    This is undefined if the list is empty.

    Complexity

    Constant.

    Returns T | undefined

lastNode

  • get lastNode(): INode<T> | null

length

  • get length(): number

size

  • get size(): number

Methods

addFirst

  • addFirst(value: T): INode<T>
  • Add a value to the beginning of the list.

    Parameters

    • value: T

      The value to add to the beginning of the list.

    Returns INode<T>

    The list node which holds the value.

    Complexity

    Constant.

addLast

  • addLast(value: T): INode<T>
  • Add a value to the end of the list.

    Parameters

    • value: T

      The value to add to the end of the list.

    Returns INode<T>

    The list node which holds the value.

    Complexity

    Constant.

assign

  • assign(values: IterableOrArrayLike<T>): void
  • Assign new values to the list, replacing all current values.

    Parameters

    • values: IterableOrArrayLike<T>

      The values to assign to the list.

      Complexity

      Linear.

    Returns void

clear

  • clear(): void

insertAfter

  • insertAfter(value: T, ref: INode<T> | null): INode<T>
  • Insert a value after a specific node in the list.

    Parameters

    • value: T

      The value to insert after the reference node.

    • ref: INode<T> | null

      The reference node of interest. If this is null, the value will be added to the end of the list.

    Returns INode<T>

    The list node which holds the value.

    Notes

    The reference node must be owned by the list.

    Complexity

    Constant.

insertBefore

  • insertBefore(value: T, ref: INode<T> | null): INode<T>
  • Insert a value before a specific node in the list.

    Parameters

    • value: T

      The value to insert before the reference node.

    • ref: INode<T> | null

      The reference node of interest. If this is null, the value will be added to the beginning of the list.

    Returns INode<T>

    The list node which holds the value.

    Notes

    The reference node must be owned by the list.

    Complexity

    Constant.

iter

  • iter(): IIterator<T>
  • Create an iterator over the values in the list.

    Returns IIterator<T>

    A new iterator starting with the first value.

    Complexity

    Constant.

nodes

  • nodes(): IIterator<INode<T>>
  • Create an iterator over the nodes in the list.

    Returns IIterator<INode<T>>

    A new iterator starting with the first node.

    Complexity

    Constant.

pop

  • pop(): T | undefined
  • Remove and return the value at the end of the list.

    Returns T | undefined

    The removed value, or undefined if the list is empty.

    Complexity

    Constant.

    Notes

    This is equivalent to removeLast.

push

  • push(value: T): void
  • Add a value to the end of the list.

    Parameters

    • value: T

      The value to add to the end of the list.

      Complexity

      Constant.

      Notes

      This is equivalent to addLast.

    Returns void

removeFirst

  • removeFirst(): T | undefined
  • Remove and return the value at the beginning of the list.

    Returns T | undefined

    The removed value, or undefined if the list is empty.

    Complexity

    Constant.

removeLast

  • removeLast(): T | undefined
  • Remove and return the value at the end of the list.

    Returns T | undefined

    The removed value, or undefined if the list is empty.

    Complexity

    Constant.

removeNode

  • removeNode(node: INode<T>): void

retro

  • retro(): IIterator<T>
  • Create a reverse iterator over the values in the list.

    Returns IIterator<T>

    A new iterator starting with the last value.

    Complexity

    Constant.

retroNodes

  • retroNodes(): IIterator<INode<T>>
  • Create a reverse iterator over the nodes in the list.

    Returns IIterator<INode<T>>

    A new iterator starting with the last node.

    Complexity

    Constant.

shift

  • shift(value: T): void
  • Add a value to the beginning of the list.

    Parameters

    • value: T

      The value to add to the beginning of the list.

      Complexity

      Constant.

      Notes

      This is equivalent to addFirst.

    Returns void

unshift

  • unshift(): T | undefined
  • Remove and return the value at the beginning of the list.

    Returns T | undefined

    The removed value, or undefined if the list is empty.

    Complexity

    Constant.

    Notes

    This is equivalent to removeFirst.

Static from

  • from<T>(values: IterableOrArrayLike<T>): LinkedList<T>
  • Create a linked list from an iterable of values.

    Type parameters

    • T

    Parameters

    • values: IterableOrArrayLike<T>

      The iterable or array-like object of interest.

    Returns LinkedList<T>

    A new linked list initialized with the given values.

    Complexity

    Linear.

Generated using TypeDoc