Options
All
  • Public
  • Public/Protected
  • All
Menu

Base class for all stacks.

Type parameters

  • T = number

Hierarchy

Index

Constructors

Properties

Accessors

Methods

Constructors

constructor

Properties

Protected _size

_size: number

Protected _stack

_stack: (null | T)[]

Protected _top

_top: number

Accessors

size

  • get size(): number
  • Returns the current filled size of the stack.

    example
    stack.push(1); // stack is an instance of a class inheriting from BaseStack
    stack.push(2);
    stack.push(3);
    stack.push(4);
    console.log(stack.size); // 4

    Returns number

Methods

clear

  • clear(): void
  • Clears the stack.

    example
    stack.push(1); // stack is an instance of a class inheriting from BaseStack
    stack.push(2);
    stack.clear();
    console.log(stack.toArray()); // []

    Returns void

isEmpty

  • isEmpty(): boolean
  • Returns true if the stack is empty, false otherwise.

    example
    console.log(stack.isEmpty()); // true // stack is an instance of a class inheriting from BaseStack
    stack.push(1);
    console.log(stack.isEmpty()); // false

    Returns boolean

peek

  • peek(): null | T
  • Returns the last element from the stack without removing it, returns null if the stack is empty.

    example
    stack.push(1); // stack is an instance of a class inheriting from BaseStack
    stack.push(2);
    console.log(stack.peek()); // 2
    stack.pop();
    stack.pop();
    console.log(stack.peek()); // null

    Returns null | T

pop

  • pop(): T
  • Pops the last element from the stack and returns it.

    example
    stack.push(1); // stack is an instance of a class inheriting from BaseStack
    stack.push(2);
    console.log(stack.pop()); // 2
    throws

    Throws an error if the stack is empty.

    example
    console.log(stack.size); // 0 // stack is an instance of a class inheriting from BaseStack
    stack.pop(); // throws Error: Stack Underflow!

    Returns T

toArray

  • toArray(): (null | T)[]
  • Returns the available elements in stack as an array.

    example
    stack.push(1); // stack is an instance of a class inheriting from BaseStack
    stack.push(2);
    console.log(stack.toArray()); // [1, 2]

    Returns (null | T)[]

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