A method rearrange that takes a queue of integers as a parameter and rearranges the order of the values so that all of t

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

A method rearrange that takes a queue of integers as a parameter and rearranges the order of the values so that all of t

Post by answerhappygod »

A Method Rearrange That Takes A Queue Of Integers As A Parameter And Rearranges The Order Of The Values So That All Of T 1
A Method Rearrange That Takes A Queue Of Integers As A Parameter And Rearranges The Order Of The Values So That All Of T 1 (84.56 KiB) Viewed 38 times
Please fill in the blank.
A method rearrange that takes 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 a stores this sequence of values: front (3, 5, 4, 17, 6, 83, 1, 84, 16, 37] back Then the call of rearrange(a); should rearrange the queue to store 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. Below is the code. What small piece of code goes in the highlighted section to make this program work. It needs to be exact to compile e.g. needs semi-colon, all lower case etc. Feel free to test in Eclipse if needed. public void rearrange (Queue<Integer> q) { Stack<Integer> s = new Stack<Integer>(); int oldSize = q.size(); for (int i = 0; i < oldSize; i++) { int n = q.remove(); if (n % 2 == 0) { q.add(n); } else { ************* 說 } } int evenCount = q.size(); while (!s.isEmpty(){ q.add(s.pop(); } for (int i = 0; i < evenCount; i++) { q.add(q.remove()); } for (int i = 0; i < oldSize - evenCount; i++) { s.push(q.remove()); } while (!s.isEmpty()) { q.add(s.pop()); } }
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply