Correct option is B
The stack elements are pushed in the following order:
- 34
- 25
- 58
- 96
- 12
Since a stack follows the LIFO (Last In First Out) principle, the original top-to-bottom order is:
12 96 58 25 34
The function reverseStack() recursively removes all elements and uses insertBottom() to insert each element at the bottom of the stack. This completely reverses the stack.
After reversal, the stack becomes:
34 25 58 96 12
The printStack() function prints elements from top to bottom, so the output will be:
Reversed Stack: 34 25 58 96 12
Important Key Points:
- A stack follows the LIFO (Last In First Out) principle.
- reverseStack() uses recursion to reverse the order of elements.
- insertBottom() inserts an element at the bottom of the stack recursively.
- After reversal, the original bottom element becomes the new top element.
Knowledge Booster:
- (a) Reversed Stack: 12 96 58 25 34: Incorrect because this is the original top-to-bottom order before reversal.
- (c) Reversed Stack: 25 34 58 96 12: Incorrect because the order after proper reversal should start with 34 at the top.
- (d) Reversed Stack: 96 58 25 34 12: Incorrect because this is not the correctly reversed sequence of the original stack.
- (e) Compilation Error: Incorrect because the program is syntactically correct and compiles successfully in C++.