const stack = [];
stack.push(1); stack.push(2);
stack.pop(); // 2 β last in, first outA stack uses push/pop for LIFO (Last In, First Out) behavior.
A Stack is a Last-In-First-Out (LIFO) data structure where elements are added and removed from the same end (the top).
Operations:
push(x): Add element x to the toppop(): Remove and return the top elementpeek(): View the top element without removingsize: Number of elementsReal-world example: Browser back button (most recent page visited is removed first).
class Stack {
push(x: any): void {}
pop(): any {}
peek(): any {}
get size(): number {}
}
const s = new Stack()
s.push(1)
s.push(2)
s.pop() // 2