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
A computer equipped with fingerprint recognition software, which denies access to a computer to anyone whose fingerprint is not
morpeh [17]

Answer: C. It is not possible for any top computer hacker to gain access to a computer equipped with the recognition software solely by virtue of skill in replicating the structure of fingerprints

Explanation:

Option A is incorrect. There's no information on the speed and analysis of the fingerprint.

Option B is incorrect. No information regarding computer installation was given in the passage.

Option C is correct. With the information, it can be concluded that it is impossible for a top hacker to have access to the protected computer.

Option D is Incorrect. Information regarding time and investment costs that were incurred during the software development wasn't given in the passage

Option E is Incorrect. The passage didn't give information on the errors that the software produced.

8 0
3 years ago
Money money money money
elena-s [515]

Answer:

i love money <3

Explanation:

8 0
3 years ago
Read 2 more answers
What stage of software development incorporates planning to help make changes in the project plan based of reviews
larisa86 [58]

Answer:

A

Explanation:

8 0
3 years ago
Assume that you have 22 slices of pizza and 7 friends that are going to share it (you've already eaten). There's been some argum
xz_007 [3.2K]

Answer:

In Python:

numberOfWholeSlices = int(22/7)

print(numberOfWholeSlices )

Explanation:

For each friend to get a whole number of slices, we need to make use of the int function to get the integer result when the number of slices is divided by the number of people.

So, the number of slice is: 22/7

In python, the expression when assigned to numberOfWholeSlices  is:

numberOfWholeSlices = int(22/7)

Next, is to print the calculated number of slices

print(numberOfWholeSlices )

4 0
3 years ago
Which header will be the largest?<br><br> Hello<br><br> Hello<br><br> Hello<br><br> Hello
geniusboy [140]

Answer:  

Bye

bye

bye

bye

Explanation:

4 0
3 years ago
Read 2 more answers
Other questions:
  • What can you do with someone's ip address?
    9·1 answer
  • What is an unique text-based internet address corresponding to a computer's unique numeric IP address called
    8·1 answer
  • What command do you type in the search box to access the command line intrface in windows?
    8·1 answer
  • What is the use of "Computer" Icon ?
    15·2 answers
  • Write the definition of a class WeatherForecast that provides the following behavior (methods): A method called set_skies that h
    13·1 answer
  • technician is dispatched to troubleshoot a user's computer. After performing diagnostics, the technician determines that drive t
    5·1 answer
  • Pls help me im confused prob more on the way
    6·1 answer
  • Select the correct answer.
    7·2 answers
  • Jazmine just finished setting up an operating system that's designed to work between a VM guest OS and computer hardware. What i
    8·1 answer
  • Which statement best explains the way that similar apps are used in different devices?
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!