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]
4 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]4 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 power supply unit for a computer converts:
Sindrei [870]
The answer should be A).
4 0
4 years ago
What OS is most commonly used by businesses?
valentinak56 [21]
Businesses commonly use windows as their OS
8 0
3 years ago
Why does a bus topology require a terminator? a) because otherwise it would be a ring network b) because otherwise the packets w
Maksim231197 [3]

Answer:

B)because otherwise the packets will bounce back and forth from end to end repeatedly

Explanation:

Bus topology can be regarded as a kind of topology for a Local Area Network, it is one that has it's nodes connected to a single cable(backbone) and any break in the so called backbone, there will be failure in the entire segment. However a Terminator is usually attached to the end-points of a bus network so that the signal is absorbed by the Terminator and as a result of this the signal will not reflect back down the line. If there is no Terminator there would be bouncing back and forth of packet in an endless loop.It should be noted that a bus topology require a terminator because otherwise the packets will bounce back and forth from end to end repeatedly

.

3 0
3 years ago
Read 2 more answers
When solving a problem in a group situation, which of the following traits should be demonstrated?
kakasveta [241]

Answer:

compromise should be demonstrated

8 0
3 years ago
DJ Davon is making a playlist for an internet radio show; he is trying to decide what 1212 songs to play and in what order they
Alja [10]

There are some typos in this question as the numbers become too large and lead to undefined during calculations.

so the correct data is:

Songs = 12

Rock = 15

Blues = 20

Disco = 15

The answer & explanation for this question is given in the attachment below.

4 0
3 years ago
Other questions:
  • Technlogies are having a negative impact on business true or false
    7·1 answer
  • Fractures in Earth’s crust where displacement has occurred are called
    13·1 answer
  • A computer can sort x objects in t seconds, as modeled by the function below:
    5·1 answer
  • Write a C++ program that stores the integers 50 and 100 in variables and stores the sum of these two in a variable named total.
    11·1 answer
  • How can you prevent your VMs receiving DHCP server messages from unauthorized virtual machine pretending to be DHCP servers?
    13·1 answer
  • What is your favorite food
    11·2 answers
  • PROGRAM DESCRIPTION: In this assignment, you will write two complete C programs that will allow two players to play the game of
    12·1 answer
  • Consider the following code:
    13·1 answer
  • Anyone want to talk? it can honestly be about any subject :)
    5·1 answer
  • Evaluate the advantages and disadvantages of cloud computing.
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!