Stack
is a Last-In-First-Out (LIFO) data structure that provides methods for adding and removing elements.
To create a new stack, simply initialize the Stack
class:
const stack = new Stack<number>();
The Stack
class constructor can take an optional configuration object:
Property | Type | Description |
---|---|---|
debug |
boolean | (Optional) Enables or disables debug mode. Defaults to false . |
When the debug
option is set to true
, the Stack
will emit events and log actions to the console, aiding in development and debugging.
Example:
const stack = new Stack<number>({ debug: true });
Adds an element to the top of the stack.
stack.push(element: T): void;
Removes and returns the top element from the stack.
stack.pop(): T | undefined;
Returns the top element from the stack without removing it.
stack.peek(): T | undefined;
Returns the number of elements in the stack.
stack.size(): number;
Checks if the stack is empty.
stack.isEmpty(): boolean;
Removes all elements from the stack.
stack.clear(): void;