Question: Show that a stack and a queue can be used to realize any permutation. That is, suppose you are given an empty stack, S, and
Show that a stack and a queue can be used to realize any permutation. That is, suppose you are given an empty stack, S, and the numbers, 1, 2,...,n, in this order, initially stored in a queue, Q. Show how to use only these two structures, and at most a constant number of additional registers, to result in any given permutation, π, of the numbers, 1, 2,...,n, stored in the Q in the order specified by π. What is the running time of your algorithm?
Step by Step Solution
3.58 Rating (165 Votes )
There are 3 Steps involved in it
Let i be the last number in Perform enough enqueue and dequeue operations to remove ... View full answer
Get step-by-step solutions from verified subject matter experts
