Step 1 — Invariant: A stack maintains a single "top" of the stack. The last element pushed is the first one to be popped (LIFO). We can implement it with an array (or linked list) by adding and removing only from one end.
Step 2 — Push: To push a value, add it to the top of the stack (e.g. append to the end of the array and increment the top index, or create a new node and set it as the new head in a linked list). The new element becomes the top. Time O(1).
Step 3 — Pop: To pop, remove and return the top element. If the stack is empty, pop is undefined or throws. After removal, the previous element (if any) becomes the new top. Time O(1).
Step 4 — Applications: Stacks are used for depth-first search (DFS), matching brackets in expressions, evaluating postfix expressions, and maintaining undo history. Peek (or top) returns the top without removing it; isEmpty checks if the stack has no elements.