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
Which of the following information sources was not directly affected by the Telecommunications Act of 1996?
VMariaS [17]
Apparently, the answer is newspaper publishing.

Since telecommunication is defined to use wireless and electrical protocols to transmit data.
6 0
3 years ago
A simple system is to be designed to allow for the selling of old books. There are two different types of users: buyers and sell
VMariaS [17]
Yes yes yes yes yeah yes
7 0
4 years ago
The structure and organization of data in a database is called a data ____.
Aleks [24]
Answer:  "hierarchy" .
_____________________________________________________
6 0
3 years ago
______ is a process that marks the location of tracks and sectors on a disk.
beks73 [17]
Answer is c low level format
3 0
3 years ago
List three considerations when you choose memory.
liq [111]
1)How many memory cards (how many can you computer hold)
2)Company (some companies run better than others)
3)Amount of memory (xGb)
8 0
3 years ago
Other questions:
  • To create an individual version of a slide, you would click
    9·1 answer
  • Cloud computing service providers manage different computing resources based on the services they offer. Which resources do IaaS
    11·1 answer
  • What is the decimal equivalent of (11000 + 10000)/101?
    10·1 answer
  • Which of the following statements is true?
    8·1 answer
  • Which line of code will print I can code on the screen?
    13·1 answer
  • Write a Java class with the following methods: getArray(int numStrings) is an instance method that takes command line input (use
    11·1 answer
  • What is alfred anderson in creole?
    6·1 answer
  • When browsing using certain browsers, if a page is known to be malicious or using phishing techniques in the past a browser may
    5·1 answer
  • Modify your main.c file so that it allocates a two dimensional array of integers so that the array has 20 rows of 30 integers in
    14·1 answer
  • What are some innovations that television has undergone since its original invention ?
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!