Answer

A stack is a linear data structure following the LIFO (Last In, First Out) principle — the last element inserted is the first one removed, like a stack of plates. Operations: push(item) — add to top: O(1); pop() — remove and return top element: O(1); peek()/top() — view top without removing: O(1); isEmpty() — check if empty: O(1); size(): O(1). Implementation: using an array (with a top pointer) or a linked list (push/pop at head). Applications: (1) Function call stack — programming languages use a call stack to track function invocations, return addresses, and local variables; (2) Undo/redo operations in editors; (3) Browser back button (history stack); (4) Expression evaluation — postfix/infix evaluation; (5) Balanced parentheses validation; (6) DFS traversal (iterative); (7) Backtracking algorithms. Stack overflow: when a recursive function calls itself too many times, exhausting the call stack (recursion depth limit). Use stacks when you need to reverse things, match brackets, or process things in LIFO order.