Stack and Queue Programming
Write a method
that accepts a queue of integers as a
parameter and rearranges the elements by alternating the elements from the first half of the queue with those
from the second half of the queue.
For example, suppose a variable
stores the following sequence of
front [1, 2, 3, 4, 5
, 6, 7, 8, 9, 10
and we make the call of
, the queue should store the following values after the call:
, 6, 2
, 7, 3
, 8, 4
, 9, 5
, 10] back
To understand the result, consider the two halves of this list.
The first half is
[1, 2, 3, 4, 5]
second half is
[6, 7, 8, 9, 10]
These are combined in an alternating fashion to form a sequence of
interleave pairs: the first values from each half (1 and 6), then the second values from each half (2 and 7),
then the third values from each half (3 and 8), and so on.
In each pair, the value from the first half appears
before the value from the second half.
The previous example uses sequential integers to make the interleaving more obvious, but the same process
can be applied to any sequence of even length.
For example, if
had instead stored these values:
front [2, 8, -5, 19
, 7, 3, 24, 42] back
Then the method would have rearranged the list to become:
, 7, 8
, 3, -5
, 24, 19
, 42] back
Your method should throw an
if the queue does not have even size.
may use one stack as auxiliary storage to solve this problem.
You may not use any other auxiliary data