Consider the following operations on an initially empty stack:
Push 10
Push 20
Pop
Push 30
Pop
Push 40
What is the final content of the stack?
A stack operates on a Last-In, First-Out (LIFO) principle. Carefully follow the sequence of push and pop operations to track the stack's content.
10, 40
40, 20
40, 10
30, 40
Stack Operations Breakdown
- Push 10: Stack = [10]
- Push 20: Stack = [10, 20]
- Pop: Removes 20, Stack = [10]
- Push 30: Stack = [10, 30]
- Pop: Removes 30, Stack = [10]
- Push 40: Stack = [10, 40]
Thus, the final content of the stack is [10, 40].
