Important Notice:

Practice-It will be discontinued as of November 1st, 2024. After this date, the website will remain online for a transitional period, but login will be restricted to University of Washington NetID authentication. This marks the next phase towards the platform's full retirement. Thank you for your use and support of the application over the years.

If you are looking for an alternative, a similar tool, CodeStepByStep, was developed independently by the original author of Practice-It, and is available at codestepbystep.com**

logo Practice-It logo

rearrange

Language/Type: Java Collections Stacks and Queues
Author: Stuart Reges (on 2014/02/13)

Write a method rearrange that takes a Queue of integers as a parameter and that 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:

    front [3, 5, 4, 17, 6, 83, 1, 84, 16, 37] back

Then the following call:

    rearrange(q);

should rearrange the order so that the queue stores the following sequence of values:

    front [4, 6, 84, 16, 3, 5, 17, 83, 1, 37] back

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 list and the order of the odds is the same as in the original list.

You are to use one stack as auxiliary storage to solve this problem.You may not use any other auxiliary data structures to solve this problem, although you can have as many simple variables as you like You also may not solve the problem recursively.

In writing your method, assume that you are using the Stack and Queue interfaces and the ArrayStack and LinkedQueue implementations discussed in lecture. As a result, values will be stored as Integer objects, not ints.

If you are running short of time, you can receive 15 points for a solution that puts all of the evens before the odds without preserving the original order of the evens and the odds. Your method should take a single parameter: the queue to rearrange.

Write your solution to rearrange below.

Type your solution here:


This is a method problem. Write a Java method as described. Do not write a complete program or class; just the method(s) above.

You must log in before you can solve this problem.


Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.