Stack
A stack is a linear data structure that follows the Last In First Out (LIFO) principle. In a stack, elements are inserted and removed from the same end, known as the top of the stack. The operations on a stack are as follows:
-
Push: Add an element to the top of the stack.
-
Pop: Remove and return the element at the top of the stack.
-
Peek: Return the element at the top of the stack without removing it.
-
isEmpty: Check if the stack is empty.
Stacks can be implemented using arrays or linked lists. The array-based implementation is more common due to its simplicity and efficiency.