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
What is the importance of the international employment​
My name is Ann [436]

Answer:

People who went overseas to work can learn different skills and technologies which can be beneficial for the development of our own country.

7 0
3 years ago
Read 2 more answers
Can be referred to as a universal network of interrelated computers which delivers a very wide variety of information and commun
nlexa [21]

Answer:

Internet

Explanation:

just took the test and got A

7 0
3 years ago
The Internet consists of interconnected computer networks throughout the world that everyone can use.
KiRa [710]
False, I'm pretty sure people in North Korea cant use it...
8 0
2 years ago
Read 2 more answers
____ is the use of networking technology to provide medical information and services.
Cerrena [4.2K]
Answer: WebMD

WebMD, with the URL https://www.webmd.com, provides credible health and medical information on the web.
3 0
3 years ago
A _____ can be destructive and can modify or delete files that may not be recoverable.
umka21 [38]
Virus

Hope this helps
6 0
3 years ago
Other questions:
  • The World Wide Web consists of interconnected computer networks throughout the world that everyone can use. True or False
    13·2 answers
  • To change to the completed application’s directory, we opened a command window and used the ________ command to change to the di
    7·1 answer
  • Each time the enter key is pressed, word creates a new paragraph. (points : 2) true false
    7·1 answer
  • As the operations manager for a supermarket chain, Mai needs to find a telecommunications technology that: enables managers in r
    9·1 answer
  • In this part, you have to implement a linked list that maintains a list of integers in sorted order. Thus, if the list contains
    13·1 answer
  • What are the top ten alternative songs of the 2000's?
    7·1 answer
  • What are horizontal and vertical page break? how and where are these inserted?​
    10·1 answer
  • Three different numbers need to be placed in order from least to greatest. For example, if the numbers are ordered 9, 16, 4, the
    15·1 answer
  • Kyle returns to work the next day and he would like to continue working on the document from yesterday. What should Kyle do?
    7·1 answer
  • eocs can be fixed locations, temporary facilities, or virtual structures with staff participating remotely.
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!