Question: Solve the previous exercise using a queue instead of stack. That is, suppose you are given an array, A, containing n numbers in order, as
Solve the previous exercise using a queue instead of stack. That is, suppose you are given an array, A, containing n numbers in order, as in the previous exercise. Describe in pseudocode an efficient algorithm for reversing the order of the numbers in A using a single for-loop that indexes through the cells of A, to insert each element into a queue, and then another for-loop that removes the elements from the queue and puts them back into A in reverse order. What is the running time of this algorithm?
Data From Previous Exercise.
Suppose you are given an array, A, containing n numbers in order. Describe in pseudocode an efficient algorithm for reversing the order of the numbers in A using a single for-loop that indexes through the cells of A, to insert each element into a stack, and then another for-loop that removes the elements from the stack and puts them back into A in reverse order. What is the running time of this algorithm?
Step by Step Solution
3.44 Rating (173 Votes )
There are 3 Steps involved in it
Solution Queue Follows FIFO principle Elements are insert... View full answer
Get step-by-step solutions from verified subject matter experts
