Question

In: Computer Science

Given a Queue of Integers with the interface: public void enqueue(Integer i) // add to end...

Given a Queue of Integers with the interface:

  public void enqueue(Integer i) // add to end
  public Integer dequeue()       // remove from front
  public boolean isEmpty()       // return true if empty

Write a method rearrange(Queue q) that takes a queue of integers as a parameter and rearranges the order of the values so that all of the even values appear before the odd values and that otherwise preserves the original order of the list.

For example, if a Queue contained

[3, 5, 4, 17, 6, 83, 1, 84, 16, 37]

after call rearrange it would contain

[4, 6, 84, 16, 3, 5, 17, 83, 1, 37]

You may use any internal data structures you chose.

Hint: I recommend even and odd queues.

Solutions

Expert Solution

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks


//required method.
public static void rearrange(Queue q) {
        // creating two queues, one for storing odd values and another for
        // storing even values. assuming Queue class is defined. if Queue is an
        // interface, then replace 'new Queue()' with any child class of Queue
        // interface (like ArrayQueue, LinkedQueue etc, depends on your
        // implemnetation)
        Queue oddQueue = new Queue();
        Queue evenQueue = new Queue();
        // looping until q is empty
        while (!q.isEmpty()) {
                // removing from value from q
                int value = q.dequeue();
                // if the removed value is odd, adding to odd queue, else adding to
                // even queue
                if (value % 2 != 0) {
                        oddQueue.enqueue(value);
                } else {
                        evenQueue.enqueue(value);
                }
        }
        // now simply dequeuing each element from even queue, enqueuing to
        // original q
        while (!evenQueue.isEmpty()) {
                q.enqueue(evenQueue.dequeue());
        }
        // then dequeuing each element from odd queue, enqueuing to
        // original q
        while (!oddQueue.isEmpty()) {
                q.enqueue(oddQueue.dequeue());
        }
        // now all even values are arranged before odd values.
}

Related Solutions

Consider the following interface: public interface Car{ public String getMake(); public void setMake(); public void honk();...
Consider the following interface: public interface Car{ public String getMake(); public void setMake(); public void honk(); public void crash(); public void drive(); } public interface Boat{ public String getMake (); public void setMake (); public void blast_horn(); public void sink(); public void move(); } 1. Create a concrete FamilyCar class from the Car interface.
java: Given the definitions: public interface ActionListener { public void actionPerformed(ActionEvent e); } public JTextField {...
java: Given the definitions: public interface ActionListener { public void actionPerformed(ActionEvent e); } public JTextField { public JTextField(){} public void setText(String text) {} public String getText() {} } Write the code to create a JButton on the South of the window and a JTextField on the North. The first time you click on the button, it should print out “hello!” on the JTextField. For the second time, should show “hello!hello!”. For the third time, should show “hello!hello!hello!”.
Given a class Stack with the interface public void push(char n) // pushes n onto stack...
Given a class Stack with the interface public void push(char n) // pushes n onto stack public char pop() // return the top of the stack, removing element from stack public boolean isEmpty() // return true if stack is empty Write a method public int removeX(Stack<Character> stack) which takes a stack of Characters, removes the occurrences of ‘X’ and returns the count of the number of Xs removed. It must restore the stack to its original order (less the Xs)....
Program in Java Create a queue class to store integers and implement following methods: 1- void...
Program in Java Create a queue class to store integers and implement following methods: 1- void enqueue(int num): This method will add an integer to the queue (end of the queue). 2- int dequeue(): This method will return the first item in the queue (First In First Out). 3- void display(): This method will display all items in the queue (First item will be displayed first). 4- Boolean isEmpty(): This method will check the queue and if it is empty,...
public void printQueue(DoublyLinkedQueue<Integer> Q){ int len = Q.size(); int k = 0; for (int i=0; i...
public void printQueue(DoublyLinkedQueue<Integer> Q){ int len = Q.size(); int k = 0; for (int i=0; i < len; ++i){ k = Q.dequeue(); Q.enqueue(k);    System.out.println(Q.dequeue()); } } What is the complexity of this code? Select one: a. O(N), N = k b. Q(1) c. Q(N2), N = Q.size() d. O(M), M = Q.size() e. None of the alternatives is correct. which one?
public class Main{ public static void main (String[] args) { Map<Integer, String> ssnMap = new HashMap<Integer,...
public class Main{ public static void main (String[] args) { Map<Integer, String> ssnMap = new HashMap<Integer, String>(); ssnMap.put (8675309,"Jenney"); ssnMap.put (42, "Answer to Everything"); ssnMap.put (8675309, "Stacy"); ssnMap.put (1006, "Peter"); System.out.println(ssnMap.get (8675309)); } } What is the output of the above code. Why?
Enter the left end and right end of a range of integers. User chooses an integer...
Enter the left end and right end of a range of integers. User chooses an integer in that range. Computer makes a guess that equals the mid of the range. User gives a feedback for each guess: 1 for too big, 2 for too small and 3 for just right. When the feedback is 1 (too big), the computer throws away any integer that is bigger than guess. When the feedback is 2 (too small), the computer discards the integers...
public class Assignment3 { public static Queue> makeQueue(double[] a){ // Each element of the given array...
public class Assignment3 { public static Queue> makeQueue(double[] a){ // Each element of the given array a must be inserted into a BTNode, // this method returns a queue of BTNodes, each node will contain a dataNode // the dataNode will have the value equal to the element of the array // count equal to the number of times that the element repeats // min and max must be equal to value. // the BTNode created must have its parent,...
Using Java The given class SetInterface.java is : public interface SetInterface<T> {    /**    *...
Using Java The given class SetInterface.java is : public interface SetInterface<T> {    /**    * Gets the current number of entries in this set.    *    * @return The integer number of entries currently in the set.    */    public int getSize();    /**    * Sees whether this set is empty.    *    * @return True if the set is empty, or false if not.    */    public boolean isEmpty();    /**    *...
Problem 5 Given a table of n integers and an integer k, make a program in...
Problem 5 Given a table of n integers and an integer k, make a program in C++ that: a) Read n b) Read the table of numbers. c) Determine the number of elements in the table less than k d) Determine the number of elements in the table equal to k e) Determine the number of elements in the table greater than k f) That displays the values found
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT