Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Stack<T>

A more practical and useful implementation of a stack which grows as needed. Inherits from BaseStack.

Type parameters

  • T

Hierarchy

Index

Constructors

Properties

Accessors

Methods

Constructors

constructor

  • new Stack<T>(): Stack<T>

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

push

  • push(item: T): void
  • Pushes an element to the stack.

    example
    const stack = new Stack();
    stack.push(1);
    console.log(stack.size); // 1

    Parameters

    • item: T

      Item to be pushed into the stack.

    Returns void

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)[]

Static fromArray

  • fromArray<T>(arr: T[]): Stack<T>
  • Returns a stack from the given array, this stack is unlimited in size

    example
    const stack = Stack.fromArray([1, 2, 3]);
    console.log(stack.pop()); // 3
    console.log(stack.pop()); // 2
    console.log(stack.pop()); // 1

    Type parameters

    • T

    Parameters

    • arr: T[]

    Returns Stack<T>

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