0 0 votes Consider the following code to reverse the elements of the Queue temp=dequeue(Queue); enqueue(temp,Queue); Reverse(Queue); Reverse(dequeue(Queue)); temp=dequeue(Queue); enqueue(temp,Queue); temp=dequeue(Queue); Reverse(Queue); enqueue(temp,Queue); None of above Programming and DS + – Shivani gaikawad 599 views answer comment Share Follow Print See 1 comment 1 1 comment reply gauravkc commented May 5, 2018 reply Follow flag 3. Draw recursion tree. It'll help. This is how 1 will execute. In 2, we are passing element from a queue as an argument which will probably throw error. And this is how 3 will execute. 0 0 replyShare Please log in or register to add a comment.
Best answer 0 0 votes Recursive algorithm for reversing the queue 1. Pop the element from the queue if it is not empty, otherwise, return empty queue 2. call reverse(queue) for remaining queue 3. push the popped element in the queue 1 2 3 4 after reversing we get, 4 3 2 1 temp = dequeue(Queue) Reverse(Queue) enqueue(temp , Queue) pankaj_vir answered May 5, 2018 • selected May 6, 2018 by Shivani gaikawad pankaj_vir comment Share Follow 0 reply Please log in or register to add a comment.