Home » Programming & Data Structure » Programming and data structure miscellaneous » Question

Programming and data structure miscellaneous

Programming & Data Structure

  1. What is the minimum number of stacks of size n required to implement a queue of size n ?
    1. one
    2. Two
    3. Three
    4. Four
Correct Option: B

2 stacks. You can even simulate a queue using only one stack. The second (temporary) stack can be simulated by the call stack of recursive calls to the insert method. The principle stays the same when inserting a new element into the queue:
⚈ You need to transfer elements from one stack to another temporary stack, to reverse their order. Then push the new element to be inserted, onto the temporary stack. Then transfer the elements back to the original stack.
⚈ The new element will be on the bottom of the stack, and the oldest element is on top (first to be popped).



Your comments will be displayed only after manual approval.