Options
All
  • Public
  • Public/Protected
  • All
Menu

Class LimitedStack<T>

A classic implementation of Stack that is limited to the size it's given at the initialization. Inherits from BaseStack.

Type parameters

  • T = number

Hierarchy

Index

Constructors

Properties

Accessors

Methods

Constructors

constructor

Properties

Private #_max

#_max: number

Protected _size

_size: number

Protected _stack

_stack: (null | T)[]

Protected _top

_top: number

Accessors

max

  • get max(): number

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

isFull

  • isFull(): boolean
  • Returns true if the stack is full, false otherwise.

    example
    const stack = new LimitedStack(3);
    stack.push(1);
    stack.push(2);
    stack.push(3);
    console.log(stack.isFull()); // true
    stack.pop();
    console.log(stack.isFull()); // 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 LimitedStack<number>(10);
    stack.push(1);
    stack.size(); // 1
    throws

    Throws an error if the stack is full.

    example
    const stack = new LimitedStack<number>(1);
    stack.push(1);
    stack.push(2); // throws Error: Stack Overflow!

    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[], capacity?: number): LimitedStack<T>
  • Returns a stack from the given array, optionally with a max siz

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

    if given an array with more elements than the max size specified.

    example
    const stack = LimitedStack.fromArray([1, 2, 3], 2); // throws Error: Stack capacity cannot be smaller than the array size!
    

    Type parameters

    • T

    Parameters

    • arr: T[]
    • Optional capacity: number

      (optional) Maximum size of the stack.

    Returns LimitedStack<T>

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