Options
All
  • Public
  • Public/Protected
  • All
Menu

Class DoublyLinkedList<T>

Functionality is the same for the most part (there are two more methods in this class) as SinglyLinkedList for the end users but implemented with nodes that has references to both next and previous node, inherits from LinkedList

Type parameters

  • T

Hierarchy

Index

Constructors

constructor

Properties

Protected _head

_head: null | DLLNode<T>

Protected _length

_length: number

Protected _tail

_tail: null | DLLNode<T>

Accessors

head

  • get head(): undefined | T
  • Returns the head's value of the list if the list is not empty or returns undefined

    example
    const list = new SinglyLinkedList<number>();
    list.insertAtEnd(1);
    list.insertAtEnd(2);
    console.log(list.head); // 1
    list.deleteFromStart();
    console.log(list.head); // 2

    Returns undefined | T

length

  • get length(): number
  • Returns the length of the list

    example
    const list = new SinglyLinkedList<number>();
    list.insertAtEnd(1);
    list.insertAtEnd(2);
    list.insertAtEnd(3);
    console.log(list.length); // 3
    list.deleteFromEnd();
    console.log(list.length); // 2

    Returns number

tail

  • get tail(): undefined | null | T
  • Returns the tail's value of the list if the list is not empty or returns undefined

    example
    const list = new SinglyLinkedList<number>();
    list.insertAtEnd(1);
    list.insertAtEnd(2);
    console.log(list.tail); // 2
    list.deleteFromEnd();
    console.log(list.tail); // 1

    Returns undefined | null | T

Methods

deleteAfter

  • deleteAfter(target: T): T
  • Delete the node after the given target value and return its value

    example
    const list = DoublyLinkedList.fromArray([1, 2, 3]);
    list.insertAfter(2, 4);
    console.log(list.deleteAfter(2)); // 3
    console.log(list.length); // 3
    console.log(list.toArray()); // [1, 2, 4]
    throws

    {@link Error} if the target value is not found

    example
    const list = new DoublyLinkedList<number>();
    list.deleteAfter(1); // throws an error
    throws

    {@link Error} if list is empty

    example
    const list = new DoublyLinkedList<number>();
    list.deleteAfter(3); // throws an error

    Parameters

    • target: T

      The value to be searched for in the list to delete the node after it

    Returns T

deleteAtIndex

  • deleteAtIndex(index: number): T
  • Delete the node at the given index and return its value

    example
    const list = DoublyLinkedList.fromArray([1, 2, 3]);
    console.log(list.deleteAtIndex(1)); // 2
    console.log(list.length); // 2
    console.log(list.toArray()); // [1, 3]
    throws

    {@link Error} if the index is out of bounds

    example
    const list = new DoublyLinkedList<number>();
    list.deleteAtIndex(1); // throws an error
    throws

    {@link Error} if list is empty

    example
    const list = new DoublyLinkedList<number>();
    list.deleteAfter(3); // throws an error

    Parameters

    • index: number

      The index of the node to be deleted

    Returns T

deleteBefore

  • deleteBefore(target: T): T
  • Delete the node before the given target value and return its value

    example
    const list = DoublyLinkedList.fromArray([1, 2, 3]);
    list.insertBefore(2, 4);
    console.log(list.deleteBefore(2)); // 1
    console.log(list.length); // 3
    console.log(list.toArray()); // [2, 4, 3]
    throws

    {@link Error} if the target value is not found

    example
    const list = new DoublyLinkedList<number>();
    list.deleteBefore(1); // throws an error
    throws

    {@link Error} if list is empty

    example
    const list = new DoublyLinkedList<number>();
    list.deleteBefore(3); // throws an error

    Parameters

    • target: T

      The value to be searched for in the list to delete the node before it

    Returns T

deleteFromEnd

  • deleteFromEnd(): T
  • Delete the last node from the list and return its value

    example
    const list = DoublyLinkedList.fromArray([1, 2, 3]);
    console.log(list.deleteFromEnd()); // 3
    console.log(list.length); // 2
    console.log(list.toArray()); // [1, 2]
    throws

    {@link Error} if the list is empty

    example
    const list = new DoublyLinkedList<number>();
    list.deleteFromEnd(); // throws an error

    Returns T

deleteFromStart

  • deleteFromStart(): T
  • delete the first node from the list and return its value

    example
    const list = DoublyLinkedList.fromArray([1, 2, 3]);
    console.log(list.deleteFromStart()); // 1
    console.log(list.length); // 2
    console.log(list.toArray()); // [2, 3]
    throws

    {@link Error} if the list is empty

    example
    const list = new DoublyLinkedList<number>();
    list.deleteFromStart(); // throws an error

    Returns T

deleteValue

  • deleteValue(target: T): T
  • Delete the node containing the given value and return its value

    example
    const list = DoublyLinkedList.fromArray([1, 2, 3, 4]);
    console.log(list.deleteValue(3)); // 3
    console.log(list.length); // 3
    console.log(list.toArray()); // [1, 2, 4]
    throws

    {@link Error} if the target value is not found

    example
    const list = new DoublyLinkedList<number>();
    list.deleteValue(1); // throws an error
    throws

    {@link Error} if list is empty

    example
    const list = new DoublyLinkedList<number>();
    list.deleteValue(3); // throws an error

    Parameters

    • target: T

      The value to be searched for in the list to delete the node containing it

    Returns T

display

  • display(mode?: "n" | "r"): void
  • Display the list in normal order or reverse order

    example
    const list = DoublyLinkedList.fromArray([1, 2, 3, 4]);
    list.display(); // 1 2 3 4
    list.display('n'); // 1 2 3 4
    list.display('r'); // 4 3 2 1
    throws

    {@link Error} if the mode is not n or r

    example
    const list = DoublyLinkedList.fromArray([1, 2, 3, 4]);
    list.display('a'); // throws an error

    Parameters

    • mode: "n" | "r" = "n"

      The mode to display the list in, either n for normal (optional) or r for reverse

    Returns void

forEachApply

  • forEachApply(callback: (value: DLLNode<T>) => void, mode?: "n" | "r"): void
  • Traverse the list and apply the given function to each node in normal order or reverse order

    example
    const list = DoublyLinkedList.fromArray([1, 2, 3, 4]);
    cons arr: number[] = [];
    list.forEachApply(node => {arr.push(node.value*3) });
    console.log(arr); // [3, 6, 9, 12]
    list.forEachApply(node => {arr.push(node.value*3) }, 'r');
    console.log(arr); // [12, 9, 6, 3]
    throws

    {@link Error} if mode is not n or r

    example
    const list = DoublyLinkedList.fromArray([1, 2, 3, 4]);
    

    Parameters

    • callback: (value: DLLNode<T>) => void
    • mode: "n" | "r" = "n"

      The mode to traverse the list in, either n for normal (optional) or r for reverse

    Returns void

insertAfter

  • insertAfter(target: T, value: T): void
  • Insert the given value after the given target value, throws an error if the target value is not found

    example
    const list = DoublyLinkedList.fromArray([1, 2, 3]);
    list.insertAfter(2, 4);
    console.log(list.length); // 4
    console.log(list.toArray()); // [1, 2, 4, 3]
    throws

    {@link Error} if the target value is not found

    example
    const list = new DoublyLinkedList<number>();
    list.insertAfter(1, 1); // throws an error
    throws

    {@link Error} if list is empty

    example
    const list = new DoublyLinkedList<number>();
    list.insertAfter(3); // throws an error

    Parameters

    • target: T

      The value to be searched for in the list to insert the value after it

    • value: T

      The value to be inserted after the target value

    Returns void

insertAtEnd

  • insertAtEnd(value: T): void

insertAtIndex

  • insertAtIndex(index: number, value: T): void
  • Insert the given value at the given index, throws an error if the index is out of bounds

    example
    const list = DoublyLinkedList.fromArray([1, 2, 3]);
    list.insertAtIndex(1, 4);
    console.log(list.length); // 4
    console.log(list.toArray()); // [1, 4, 2, 3]
    throws

    {@link Error} if the index is out of bounds

    example
    const list = new DoublyLinkedList<number>();
    list.insertAtIndex(1, 1); // throws an error

    Parameters

    • index: number

      index at which the value should be inserted

    • value: T

      value to be inserted at the given index

    Returns void

insertAtStart

  • insertAtStart(value: T): void

insertBefore

  • insertBefore(target: T, value: T): void
  • Insert the given value before the given target value, throws an error if the target value is not found

    example
    const list = DoublyLinkedList.fromArray([1, 2, 3]);
    list.insertBefore(2, 4);
    console.log(list.length); // 4
    console.log(list.toArray()); // [1, 4, 2, 3]
    throws

    {@link Error} if the target value is not found

    example
    const list = new DoublyLinkedList<number>();
    list.insertBefore(1, 1); // throws an error

    Parameters

    • target: T

      The value to be searched for in the list to insert the value before it

    • value: T

      The value to be inserted before the target value

    Returns void

reverse

  • reverse(): void

toArray

  • toArray(mode?: "n" | "r"): T[]
  • Get the list in a JS array in normal order or in reverse order

    example
    const list = DoublyLinkedList.fromArray([1, 2, 3, 4]);
    console.log(list.toArray('n')); // [1, 2, 3, 4]
    console.log(list.toArray('r')); // [4, 3, 2, 1]
    throws

    {@link Error} if mode is not n or r

    example
    const list = DoublyLinkedList.fromArray([1, 2, 3, 4]);
    console.log(list.toArray('a')); // throws an error

    Parameters

    • mode: "n" | "r" = "n"

      The mode to get the list in, either n for normal (optional) or r for reverse

    Returns T[]

Static fromArray

Generated using TypeDoc, the 1/4/2022 at 10:37:56 PM