Why a Stack?
LIFO (Last In, First Out) property: This mirrors the behavior of recursive function calls, where the most recent function call is the first to finish.
Simulates function call stack: In recursion, the system implicitly uses a stack to keep track of function calls and their return addresses. A non-recursive implementation can explicitly use a stack to mimic this behavior.
How it works:
Push function arguments and local variables onto the stack: Before processing a recursive call, store relevant information on the stack.
Process the current iteration: Handle the current problem without making recursive calls.
Pop values from the stack for the next iteration: Retrieve stored information to simulate returning from a recursive call.
By using a stack, you can effectively transform a recursive algorithm into an iterative one, often improving performance and memory efficiency.
If you think there is any confusion in the given MCQs, you can comment below.
docmcqs.com is Pakistan's No.1 online platform for preparing for all types of exams including PPSC, FPSC, KPSC, SPSC, Ministry of Defence, and for one-paper MCQs. This includes Pedagogy MCQs, general knowledge MCQs, current affairs MCQs, and much more.