#C8551. Reverse Queue Recursively
Reverse Queue Recursively
Reverse Queue Recursively
Given a queue of integers, your task is to reverse the elements using a recursive algorithm. You must not use loops or any iterative approach to perform the reversal—instead, use recursion as the primary mechanism. The reversal should be done in-place, modifying the original queue.
The recursive approach works as follows:
- Base Case: If the queue is empty, return.
- Otherwise, remove the front element.
- Recursively reverse the remaining queue.
- Enqueue the removed element at the back of the queue.
The final queue will have its elements in reverse order. For example, if the input queue is [1, 2, 3, 4, 5], the output should be [5, 4, 3, 2, 1].
inputFormat
The first line contains an integer (n) (where (n \ge 0)) representing the number of elements in the queue. The second line contains (n) space-separated integers which are the elements of the queue in their original order.
outputFormat
Output the elements of the reversed queue in a single line separated by a single space. If the queue is empty, print nothing.## sample
5
1 2 3 4 5
5 4 3 2 1