A LIFO queue, where the last element to be pushed into the queue is the first element to be popped out of it.

import { Stack } from "scl"

Pushing and popping an element are both in O(1).

Property name Worst-case
add() O(1)
peek() O(1)
pop() O(1)
size O(1)

Type Parameters

  • T

    The type of element in this queue.

Hierarchy (View Summary)

Implements

Constructors

  • Construct a singly-linked list.

    const l = new SingleLinkedList();
    

    You can also constrcut a linked list from any iterable, like so:

    const l = new SingleinkedList([1, 2, 3])
    

    Type Parameters

    • T

    Parameters

    • Optionaliterable: Iterable<T, any, any>

    Returns Stack<T>

Accessors

  • get size(): number
  • Count the amount of elements in the collection.

    ⚠️ In most cases, this should be an O(1) operation. However, there are cases where this can be an O(n) operation. Therefore, it is recommended to always cache the result in a local variable.

    Returns number

Methods

  • Returns an object which is able to sift through the values in this collection.

    The order by which the elements are traversed depends on the kind of collection. For unordered collections, the iteration order is unspecified and may even differ between two iterations on the same collection.

    Returns IterableIterator<T, any, any>

  • Add an element to the collection. If the element already exists, update its value.

    The location where the element is placed depends on the collection type, and in the generic case there is no guarantee on the location where it is inserted.

    This method returns a pair with the first element indicating whether the element was added, while the second element refers to the actual location of the element.

    Parameters

    • element: T

    Returns [boolean, SingleLinkedListCursor<T>]

  • Append an item at the end of the collection. The element will be given the highest order.

    Parameters

    • el: T

    Returns SingleLinkedListCursor<T>

  • Return a cursor that is placed at the index given by position in the sequence.

    Parameters

    • position: number

    Returns SingleLinkedListCursor<T>

  • Remove an element from the collection. If multiple elements are matched, the collection picks one of them.

    Parameters

    • element: T

    Returns boolean

    true if the element was found, false otherwise.

  • Allows taking an element directly out of the collection at a given position.

    This method might be faster than at because it is not forced to construct a cursor object.

    Parameters

    • index: number

    Returns T

  • Insert an element after the element at the given position. The position is deduced from the iterator that is given to the method.

    Parameters

    • pos: SingleLinkedListCursor<T>
    • el: T

    Returns SingleLinkedListCursor<T>

  • Insert an element before the element at the given position. The position is deduced from the iterator that is goven to the method.

    Parameters

    • pos: SingleLinkedListCursor<T>
    • el: T

    Returns SingleLinkedListCursor<T>

  • Get the next element in the queue without removing it from the collection.

    Returns T

    Queuelike.pop()

  • Get the next element in the order defined by the queue and remove it from the collection.

    Returns T

    Queuelike.peek()

  • Prepend an item to the beginning of the collection. The element will be given the lowest order.

    Parameters

    • el: T

    Returns SingleLinkedListCursor<T>