Class Stack<E, R>

  1. Last In, First Out (LIFO): The core characteristic of a stack is its last in, first out nature, meaning the last element added to the stack will be the first to be removed.
  2. Uses: Stacks are commonly used for managing a series of tasks or elements that need to be processed in a last in, first out manner. They are widely used in various scenarios, such as in function calls in programming languages, evaluation of arithmetic expressions, and backtracking algorithms.
  3. Performance: Stack operations are typically O(1) in time complexity, meaning that regardless of the stack's size, adding, removing, and viewing the top element are very fast operations.
  4. Function Calls: In most modern programming languages, the records of function calls are managed through a stack. When a function is called, its record (including parameters, local variables, and return address) is 'pushed' into the stack. When the function returns, its record is 'popped' from the stack.
  5. Expression Evaluation: Used for the evaluation of arithmetic or logical expressions, especially when dealing with parenthesis matching and operator precedence.
  6. Backtracking Algorithms: In problems where multiple branches need to be explored but only one branch can be explored at a time, stacks can be used to save the state at each branching point.

Type Parameters

  • E = any
  • R = any

Hierarchy (view full)

Accessors

  • get elements(): E[]
  • The elements function returns the elements of this set.

    Returns E[]

    An array of elements

  • get size(): number
  • The size() function returns the number of elements in an array.

    Returns number

    The size of the elements array.

  • get toElementFn(): undefined | ((rawElement: R) => E)
  • The function returns the _toElementFn property, which is a function that converts a raw element to a specific type.

    Returns undefined | ((rawElement: R) => E)

    The function get toElementFn() is returning either a function that takes a raw element rawElement of type R and returns an element E, or undefined if no function is assigned to _toElementFn.

Methods

  • Time Complexity: O(n) Space Complexity: O(n)

    Custom iterator for the Stack class.

    Returns IterableIterator<E, any, any>

    An iterator object.

  • Time Complexity: O(n) Space Complexity: O(1)

    The function is an implementation of the Symbol.iterator method that returns an IterableIterator.

    Parameters

    • Rest...args: any[]

      The args parameter in the code snippet represents a rest parameter. It allows the function to accept any number of arguments as an array. In this case, the args parameter is used to pass any number of arguments to the _getIterator method.

    Returns IterableIterator<E, any, any>

  • Time Complexity: O(1) Space Complexity: O(1)

    The clear function clears the elements array.

    Returns void

  • Time Complexity: O(n) Space Complexity: O(n)

    The clone() function returns a new Stack object with the same elements as the original stack.

    Returns Stack<E, R>

    The clone() method is returning a new Stack object with a copy of the _elements array.

  • The delete function removes an element from the stack.

    Parameters

    • element: E

    Returns boolean

    A boolean value indicating whether the element was successfully deleted or not

  • The deleteAt function deletes the element at a given index.

    Parameters

    • index: number

    Returns boolean

    A boolean value

  • Time Complexity: O(n) Space Complexity: O(1)

    The every function checks if every element in the array satisfies a given predicate.

    Parameters

    • predicate: ElementCallback<E, R, boolean, Stack<E, R>>

      The predicate parameter is a callback function that takes three arguments: the current element being processed, its index, and the array it belongs to. It should return a boolean value indicating whether the element satisfies a certain condition or not.

    • OptionalthisArg: any

      The thisArg parameter is an optional argument that specifies the value to be used as this when executing the predicate function. If thisArg is provided, it will be passed as the this value to the predicate function. If thisArg is

    Returns boolean

    The every method is returning a boolean value. It returns true if every element in the array satisfies the provided predicate function, and false otherwise.

  • Time Complexity: O(n) Space Complexity: O(n)

    The filter function creates a new stack containing elements from the original stack that satisfy a given predicate function.

    Parameters

    • predicate: ElementCallback<E, R, boolean, Stack<E, R>>

      The predicate parameter is a callback function that takes three arguments: the current element being iterated over, the index of the current element, and the stack itself. It should return a boolean value indicating whether the element should be included in the filtered stack or not.

    • OptionalthisArg: any

      The thisArg parameter is an optional argument that specifies the value to be used as this when executing the predicate function. If thisArg is provided, it will be passed as the this value to the predicate function. If thisArg is

    Returns Stack<E, R>

    The filter method is returning a new Stack object that contains the elements that satisfy the given predicate function.

  • Time Complexity: O(n) Space Complexity: O(1)

    The find function iterates over the elements of an array-like object and returns the first element that satisfies the provided callback function.

    Parameters

    • callbackfn: ElementCallback<E, R, boolean, Stack<E, R>>

      The callbackfn parameter is a function that will be called for each element in the array. It takes three arguments: the current element being processed, the index of the current element, and the array itself. The function should return a boolean value indicating whether the current element matches the desired condition.

    • OptionalthisArg: any

      The thisArg parameter is an optional argument that specifies the value to be used as this when executing the callbackfn function. If thisArg is provided, it will be passed as the this value to the callbackfn function. If thisArg @returns The findmethod returns the first element in the array that satisfies the provided callback function. If no element satisfies the callback function,undefined` is returned.

    Returns undefined | E

  • Time Complexity: O(n) Space Complexity: O(1)

    The forEach function iterates over each element in an array-like object and calls a callback function for each element.

    Parameters

    • callbackfn: ElementCallback<E, R, void, Stack<E, R>>

      The callbackfn parameter is a function that will be called for each element in the array. It takes three arguments: the current element being processed, the index of the current element, and the array that forEach was called upon.

    • OptionalthisArg: any

      The thisArg parameter is an optional argument that specifies the value to be used as this when executing the callbackfn function. If thisArg is provided, it will be passed as the this value to the callbackfn function. If `thisArg

    Returns void

  • Time Complexity: O(n) Space Complexity: O(1)

    The function checks if a given element exists in a collection.

    Parameters

    • element: E

      The parameter "element" is of type E, which means it can be any type. It represents the element that we want to check for existence in the collection.

    Returns boolean

    a boolean value. It returns true if the element is found in the collection, and false otherwise.

  • The function checks if an array is empty and returns a boolean value.

    Returns boolean

    A boolean value indicating whether the _elements array is empty or not.

  • Time Complexity: O(n) Space Complexity: O(n)

    The map function takes a callback function and applies it to each element in the stack, returning a new stack with the results.

    Type Parameters

    • EM
    • RM

    Parameters

    • callback: ElementCallback<E, R, EM, Stack<E, R>>

      The callback parameter is a function that will be called for each element in the stack. It takes three arguments: the current element, the index of the element, and the stack itself. It should return a new value that will be added to the new stack.

    • OptionaltoElementFn: ((rawElement: RM) => EM)

      The toElementFn parameter is an optional function that can be used to transform the raw element (RM) into a new element (EM) before pushing it into the new stack.

        • (rawElement): EM
        • Parameters

          • rawElement: RM

          Returns EM

    • OptionalthisArg: any

      The thisArg parameter is an optional argument that allows you to specify the value of this within the callback function. It is used to set the context or scope in which the callback function will be executed. If thisArg is provided, it will be used as the value of

    Returns Stack<EM, RM>

    a new Stack object with elements of type EM and raw elements of type RM.

  • Time Complexity: O(1) Space Complexity: O(1)

    The peek function returns the last element of an array, or undefined if the array is empty.

    Returns undefined | E

    The peek() function returns the last element of the _elements array, or undefined if the array is empty.

  • Time Complexity: O(1) Space Complexity: O(1)

    The pop function removes and returns the last element from an array, or returns undefined if the array is empty.

    Returns undefined | E

    The pop() method is returning the last element of the array _elements if the array is not empty. If the array is empty, it returns undefined.

  • Time Complexity: O(1) Space Complexity: O(1)

    The push function adds an element to the stack and returns the updated stack.

    Parameters

    • element: E

      The parameter "element" is of type E, which means it can be any data type.

    Returns boolean

    The push method is returning the updated Stack<E> object.

  • Time Complexity: O(n) Space Complexity: O(1)

    The reduce function iterates over the elements of an array-like object and applies a callback function to reduce them into a single value.

    Type Parameters

    • U

    Parameters

    • callbackfn: ReduceElementCallback<E, R, U, Stack<E, R>>

      The callbackfn parameter is a function that will be called for each element in the array. It takes four arguments:

    • initialValue: U

      The initialValue parameter is the initial value of the accumulator. It is the value that the accumulator starts with before the reduction operation begins.

    Returns U

    The reduce method is returning the final value of the accumulator after iterating over all the elements in the array and applying the callback function to each element.

  • Time Complexity: O(n) Space Complexity: O(1)

    The "some" function checks if at least one element in a collection satisfies a given predicate.

    Parameters

    • predicate: ElementCallback<E, R, boolean, Stack<E, R>>

      The predicate parameter is a callback function that takes three arguments: value, index, and array. It should return a boolean value indicating whether the current element satisfies the condition.

    • OptionalthisArg: any

      The thisArg parameter is an optional argument that specifies the value to be used as the this value when executing the predicate function. If thisArg is provided, it will be passed as the this value to the predicate function. If `thisArg

    Returns boolean

    a boolean value. It returns true if the predicate function returns true for any element in the collection, and false otherwise.

  • Time Complexity: O(n) Space Complexity: O(n)

    The toArray function returns a copy of the elements in an array.

    Returns E[]

    An array of type E.

  • Time Complexity: O(n) Space Complexity: O(n)

    The function "fromArray" creates a new Stack object from an array of elements.

    Type Parameters

    • E

    Parameters

    • elements: E[]

      The elements parameter is an array of elements of type E.

    Returns Stack<E, any>

    The method is returning a new instance of the Stack class, initialized with the elements from the input array.