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

Constructors

constructor

Properties

Private _first

_first: LinkedListNode<T> | null = null

Private _last

_last: LinkedListNode<T> | null = null

Private _length

_length: 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
  • The first node in the list.

    This is null if the list is empty.

    Complexity

    Constant.

    Returns INode<T> | null

isEmpty

  • get isEmpty(): boolean
  • Whether the list is empty.

    Complexity

    Constant.

    Returns 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
  • The last node in the list.

    This is null if the list is empty.

    Complexity

    Constant.

    Returns INode<T> | null

length

  • get length(): number
  • The length of the list.

    Complexity

    Constant.

    Returns 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.

clear

  • clear(): void
  • Remove all values from the list.

    Complexity

    Linear.

    Returns 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.

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
  • Remove a specific node from the list.

    Parameters

    • node: INode<T>

      The node to remove from the list.

      Complexity

      Constant.

      Notes

      The node must be owned by the list.

    Returns 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.

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.

Generated using TypeDoc