@monocircuit/utils
Version:
A repository that contains data structures, algorithms and more.
83 lines (82 loc) • 2.63 kB
TypeScript
/**
* A representation of options that can be passed to the `Stack`
* constructor.
*/
declare type StackConstructorOptions<D extends unknown = number> = Partial<{
/**
* An array that will be put in the form of a `Stack` and used as an
* initial value of the instance.
*/
array: Array<D>;
}>;
/**
* A linear data structure that is build in a fashion that mostly exposes a
* BigO notation of `O(1)`
*
* It follows the principle of LIFO (Last In First Out).
*
* @spacecomplexity `O(n)`
*/
declare class Stack<D extends unknown = number> {
/**
* The `LinkedList` that serves as a basis of all operations of the `Stack`.
*/
private __linkedList;
/**
* @param options Options that affect the initialization process of the `Stack`.
*/
constructor(options?: StackConstructorOptions<D>);
/**
* Returns the amout of elements found on the `Stack`.
* @returns The length of the `Stack`
*/
get length(): number;
/**
* Checks wether or not the `Stack` does not contain any elements. This
* works based on the `LinkedList` implementation.
*
* @returns A boolean that indicates wether or not the `Stack` is empty
*/
isEmpty(): boolean;
/**
* Fetches the element that currently sits on top of the `Stack`.
*
* @returns The data that the top most element holds
*/
peek(): D | null;
/**
* Adds a new element with some data to the top of the `Stack`.
*
* @param data A piece of data that will be held by the element on the `Stack`
* @returns The `Stack` instance
*/
push(data: D): this;
/**
* Deletes the top most element of the `Stack`.
*
* @returns The data that the deleted element held, or nothing if the `Stack` was empty.
*/
pop(): D | null;
/**
* Converts the `Stack` to an array. The indices are converted as to be
* expected.
*
* @returns An array containing the elements of the `Stack`
*/
toArray(): D[];
/**
* Prepends an array to the `Stack` instance.
*
* @param data An array of data that will be put on the `Stack`
* @returns The `Stack` instance
*/
fromArray(data: D[]): this;
/**
* Converts the `Stack` to string.
*
* @param callback A function that will handle the stringification
* @returns A string containg the stringified data of the stack's elements.
*/
toString(callback?: (data: D) => string): string;
}
export default Stack;