Reversing a Queue - cook the code

Friday, 29 December 2017

Reversing a Queue

Reversing a Queue

Give an algorithm for reversing a queue Q. Only following standard operations are allowed on queue.
  1. enqueue(x) : Add an item x to rear of queue.
  2. dequeue() : Remove an item from front of queue.
  3. empty() : Checks if a queue is empty or not.
Examples:
Input : Q = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
Output :Q = [100, 90, 80, 70, 60, 50, 40, 30, 20, 10]

Input :[1, 2, 3, 4, 5]
Output :[5, 4, 3, 2, 1]
Output:
 50 40 30 20 10 60 70 80 90 100

No comments:

Post a Comment