1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
docker41 [41]
3 years ago
7

Write a method called rearrange that accepts a queue of integers as a parameter and rearranges the order of the values so that a

ll of the even values appear before the odd values and that otherwise preserves the original order of the queue. For example, if the queue stores [3, 5, 4, 17, 6, 83, 1, 84, 16, 37], your method should rearrange it to store [4, 6, 84, 16, 3, 5, 17, 83, 1, 37]. Notice that all of the evens appear at the front followed by the odds and that the relative order of the evens and odds is the same as in the original. Use one stack as auxiliary storage.
Computers and Technology
1 answer:
Dafna11 [192]3 years ago
4 0

Answer:

See explaination

Explanation:

import java.util.*;

public class qs {

public static void rearrange(Queue<Integer> q)

{

int n = q.size();

Stack<Integer> st = new Stack<Integer>();

Integer f;

for(int i = 0; i < n ; i++)

{

f = q.poll();

// Even elements are added back to the list

if(f%2==0)

q.add(f);

else

st.push(f);

}

// Odd elements are added to the list in reverse order

while(st.size()>0)

{

q.add(st.pop());

}

// Repeats the above process to correct the order of odd elements

for(int i = 0; i < n ; i++)

{

f = q.poll();

// Even elements are added back to the list

if(f%2==0)

q.add(f);

else

st.push(f);

}

//Order of Odd elements are reversed so as to match the actual order

while(st.size()>0)

{

q.add(st.pop());

}

}

public static void main(String[] args) {

int arr[] = {3, 5, 4, 17, 6, 83, 1, 84, 16, 37};

int n = arr.length;

Queue<Integer> q = new LinkedList<Integer>();

for(int i = 0;i<n;i++)

q.add(arr[i]);

System.out.print("\nOriginal Queue\n");

System.out.println(q.toString());

rearrange(q);

System.out.print("\nReordered Queue\n");

System.out.println(q.toString());

}

}

You might be interested in
Razer blade 15 1070 max q 144 hz ( 2018 ) vs Alienware m15 r2 luna white 1660ti 144 hz
kicyunya [14]

Answer:

I believe razor blade 15 1070

Explanation:

Because it seems more sleak

3 0
3 years ago
A studio camera is generally small and lightweight enough to be taken out into the
inysia [295]
I think the correct answer should be true
6 0
3 years ago
ARPAnet was the precursor to today's Internet. TRUE or FALSE.
ruslelena [56]
The ARPANET was the precursor to the internet.
8 0
3 years ago
does someone have honey downloaded, can I send yall a link and y'all run honey on it and just tell me the final product with the
Colt1911 [192]

send me link and i can do it for you

3 0
3 years ago
When you save a file to the cloud, it means your file stored _________
choli [55]
<span>on an Internet-connected, remote computer, rather than your own device. Some examples of cloud-based software include Dropbox and Google Drive. One major benefit of the cloud is that you can access all of your data (for example, your photographs, music, schoolwork, or other important files) safely and securely on any device that has the ability to access it. For example, if you are working on an assignment at school but run out of time, you can save your work and access it anywhere else you can access the cloud, such as your home or library.</span>
8 0
3 years ago
Other questions:
  • A slide in a presentation program can have which of the following?
    5·2 answers
  • Read "Penetration Testing – Reconnaissance with NMAP Tool," located within the required readings. In the article, the authors di
    10·1 answer
  • Differentiate Between register and Memory location (
    13·1 answer
  • Write a program that reads from the user any three points in two dimensional space: x1, y1, x2, y2, x3, y3. Assume these points
    13·1 answer
  • While developing a network application, a programmer adds functionally that allows her to access the running program without aut
    14·1 answer
  • The question is inside the screenshot
    13·1 answer
  • Write a loop that continually asks the user what food the user has in their refrigerator until the user enters apples, in which
    10·1 answer
  • What action should you take when using removable media in a scif?
    15·1 answer
  • • R7.9 Write enhanced for loops for the following tasks.
    13·1 answer
  • What is the difference between concrete language and abstract language? give an example of each.
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!