Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Stack<TValue>

A last-in-first-out (LIFO) data structure.

remarks

For more information about stacks: https://en.wikipedia.org/wiki/Stack_(abstract_data_type).

Type parameters

  • TValue

    The type of data the stack holds

Hierarchy

  • Stack

Index

Constructors

Accessors

Methods

Constructors

constructor

  • new Stack(...items: TValue[]): Stack
  • Creates a stack.

    If items is not null, it will push items onto the stack:

    const stack1 = new Stack<number>(1, 2, 3);
    const stack2 = new Stack<number>().push(1).push(2).push(3);

    Both stacks will be equal.

    Parameters

    • Rest ...items: TValue[]

      The items to push onto the stack

    Returns Stack

Accessors

size

  • get size(): number

Methods

peek

  • peek(): TValue | null
  • Returns a value off the top of the stack without modifying the stack.

    const stack = new Stack(1, 2, 3);
    
    console.log(stack.peek()); // 3
    console.log(stack.size); // 3

    Returns TValue | null

    The value off the top of the stack

pop

  • pop(): TValue | null
  • Pops a value off the top of the stack, and then removes it.

    const stack = new Stack(1, 2, 3);
    
    console.log(stack.pop()); // 3
    console.log(stack.size); // 2

    Returns TValue | null

    The value off the top of the stack

push

  • push(value: TValue): this
  • Pushes a value on to the top of the stack.

    Since push returns this, multiple push commands can be chained:

    const stack = new Stack<number>();
    
    stack.push(1).push(2).push(3);

    Parameters

    • value: TValue

      The value to push on to the top of the stack

    Returns this

    This stack

toArray

  • toArray(): TValue[]
  • Returns an array representation of the stack.

    Returns TValue[]

    An array representation of the stack

toString

  • toString(): string
  • Returns a string representation of the stack.

    Returns string

    A string representation of the stack

Static create

  • create<TValue>(...items: TValue[]): Stack<TValue>
  • Creates a stack.

    If items is not null, it will push items onto the stack:

    const stack1 = Stack<number>.create(1, 2, 3);
    const stack2 = Stack<number>.create().push(1).push(2).push(3);

    Both stacks will be equal.

    Type parameters

    • TValue

      The type of data the stack holds

    Parameters

    • Rest ...items: TValue[]

      The items to push onto the stack

    Returns Stack<TValue>

    A new stack

Generated using TypeDoc