logo CodeStepByStep logo

rearrange

Language/Type: C++ Collections stacks and queues STL
Related Links:

Write a function named rearrange that takes a reference to a queue of integers as a parameter and rearranges the order of the values so that all of the even values appear before the odd values and that otherwise preserves the original order of the list. For example, suppose a queue called q stores this sequence of values:

{3, 5, 4, 17, 6, 83, 1, 84, 16, 37}

The call of rearrange(q); should rearrange the queue to store the following sequence of values:

{4, 6, 84, 16, 3, 5, 17, 83, 1, 37}

Notice that all of the evens appear at the front of the queue followed by the odds and that the order of the evens is the same as in the original queue and the order of the odds is the same as in the original queue. You may use one stack as auxiliary storage.

Function: Write a C++ function as described, not a complete program.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.