A stack is a linear data structure that follows the Last In, First Out (LIFO) principle. It supports the following primary operations:
Push: Insert an element at the top of the stack.
Pop: Remove and return the top element of the stack.
Peek/Top: Retrieve the top element without removing it.
isEmpty: Check if the stack is empty.
Time Complexity:
Push, Pop, Peek: O(1) (constant time, as operations only involve the top element) isEmpty: O(1)
Space Complexity:
O(n), where n is the number of elements in the stack. Stack can be implemented using arrays or linked lists, but arrays are generally more efficient in terms of cache performance.