Stack<T>
Represents a stack data structure that follows the Last-In-First-Out (LIFO) principle. It uses a singly linked list internally for efficient push and pop operations.
T
- The type of elements in the stack.
constructor();
Creates a new instance of the Stack class. The underlying data structure is a singly linked list.
get isEmpty(): boolean;
Checks if the stack is empty.
Returns: boolean
- True if the stack is empty, false otherwise.
get size(): number;
Returns the number of elements in the stack.
Returns: number
- The size of the stack.
get top(): T | undefined;
Retrieves the value at the top of the stack.
Returns: T | undefined
The value at the front of the queue, or undefined if the queue is empty.
clear(): void;
Removes all elements from the stack.
Returns: void
peek(): T | undefined;
Retrieves the value at the top of the stack without removing it.
Returns: T | undefined
The value at the top of the stack, or undefined if the stack is empty.
pop(): T | undefined;
Removes and returns the element from the top of the stack.
Returns: T | undefined
The value popped from the stack, or undefined if the stack is empty.
push(val: T): void;
Adds a new element to the top of the stack.
Parameters:
val
- The value to be pushed onto the stack.
Returns: void
toArray(): T[];
Returns an array representation of the stack.
Returns: T[]
An array containing the elements of the stack in top-to-bottom order.