Question

In java please: Write a method that accepts as a parameter a queue of integers that...

In java please:

Write a method that accepts as a parameter a queue of integers that are already sorted by absolute value, and modifies it so that the integers are sorted normally. Only use a single stack as auxiliary storage. For example, if a queue variable named q stores the following elements:

front {1, -2, 4, 5, -7, -9, -12, 28, -34} back
Then the call of reorder(q); should modify it to store the following values:

front {-34, -12, -9, -7, -2, 1, 4, 5, 28} back

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution :

import java.util.*;
class Solution
{
   // This method accepts a parameter a queue of integers that are already sorted by absolute value, and modifies it so that the integers are sorted normally.
   public void reorder(Queue<Integer> q)
   {
       // creating a single stack of Integer as auxiliary storage
       Stack<Integer> s = new Stack<Integer>();
      
       // getting the size of the given queue
       int size = q.size();
      
       // this is a temporary variable which will store the count of positive values of the given queue
       int count = 0;          
      
       // iterating for all the values of the given queue
       for(int i = 0; i < size; i++)
       {
           int front_val = q.remove();               // getting the front value of the given queue and removing it
          
           // if the front value of the given queue is negative, then push it to the stack
           if(front_val < 0)           
               s.push(front_val);
          
           // else adding the front value to the back of the queue again
           else if( front_val >= 0 )
           {
               q.add(front_val);
               count++;               // also incrementing the count of positive values of the given queue
           }
       }
      
       // adding all the elements of the stack one by one to the back of the given queue
       while(!s.isEmpty())
           q.add(s.pop());
      
       // now adding all the positive values of the queue to the back of the queue again
       for(int i = 0; i < count; i++)
           q.add(q.remove());
      
       // printing all the values of the queue in normally sorted order
       System.out.print("\nSorted Elements are : " + q );
      
       // printing a new line
       System.out.println();
   }
  
   // this part of the code is only for checking the correctness of the output
   public static void main(String args[])
   {
       // creating a queue of Integer
       Queue<Integer> q = new LinkedList<>();
      
       // adding all the given elements to the queue ( only for checking the correctness of the code )
       q.add(1);
       q.add(-2);
       q.add(4);
       q.add(5);
       q.add(-7);
       q.add(-9);
       q.add(-12);
       q.add(28);
       q.add(-34);
      
       // creating an object for calling reorder() to get the expected output
       Solution obj = new Solution();
      
       // calling reorder() to get the expected output as in the given question and passing q to it
       obj.reorder(q);
   }
}

Output :

Add a comment
Know the answer?
Add Answer to:
In java please: Write a method that accepts as a parameter a queue of integers that...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a method called reverseFirstK that accepts an integer k and a queue of integers as...

    Write a method called reverseFirstK that accepts an integer k and a queue of integers as parameters and reverses the order of the first k elements of the queue, leaving the other elements in the same relative order. For example, if a queue named q stores [10, 20 30, 40, 50, 60, 70, 80, 90], the call of reverseFirstK (4, q):should change the queue to store [40, 30 20, 10, 50, 60, 70, 80, 90]. If k is 0 or...

  • Java Program Write a method called reverseBottomHalf that accepts a Stack of integers as a parameter...

    Java Program Write a method called reverseBottomHalf that accepts a Stack of integers as a parameter and reverses only the values in the bottom half of the Stack. For example, if a Stack containing the values [1, 2, 3, 4, 5] were passed in (with 1 at the bottom and 5 at the top), the Stack would be changed to [2, 1, 3, 4, 5] (with 2 at the bottom and 5 at the top) after this method was called...

  • Write a method maxOccurrences that accepts a list of integers as a parameter and returns the...

    Write a method maxOccurrences that accepts a list of integers as a parameter and returns the number of times the most frequently occurring integer (the “mode”) occurs in the list. Solve this problem using a map as auxiliary storage. If the list is empty, return 0.

  • Write a method called removeDuplicates that accepts a PriorityQueue of integers as a parameter and modifies...

    Write a method called removeDuplicates that accepts a PriorityQueue of integers as a parameter and modifies the queue’s state so that any element that is equal to another element in the queue is removed. For example, if the queue stores [7, 7, 8, 8, 8, 10, 45, 45], your method should modify the queue to store [7, 8, 10, 45]. You may use one stack or queue as auxiliary storage. Please also create a Main Program to test the code....

  • Write a java method merge that accepts two arrays of integers and returns a new array...

    Write a java method merge that accepts two arrays of integers and returns a new array containing all elements of the first array followed by all elements of the second. int[] a1 = {12, 34, 56}; int[] a2 = {7, 8, 9, 10};   int[] a3 = merge(a1, a2); System.out.println(Arrays.toString(a3)); // [12, 34, 56, 7, 8, 9, 10]

  • Write in Java (10 pts) Write a public method named “Even_Sum” that accepts a one-dimensional array...

    Write in Java (10 pts) Write a public method named “Even_Sum” that accepts a one-dimensional array of integers and returns the sum of the even elements in the array (10 pts) Write a public method named “Reverse” that receives a string prints its contents in reverse to the standard output using a stack. Assume the stack has been created and is empty.

  • Page ot 9 2. Stacks/Queues: Write a method splitStack that takes a stack of integers as...

    Page ot 9 2. Stacks/Queues: Write a method splitStack that takes a stack of integers as a parameter and splits it into negatives and non-negatives. The numbers in the stack should be rearranged so that all the negatives appear on the bottom of the stack and all the non-negatives appear on the top. In other words, if after this method is called you were to pop numbers off the stack, you would first get all the nonnegative numbers and then...

  • in java Write a class named Palindrome.java and Write a method isPalindrome that takes an IntQueue...

    in java Write a class named Palindrome.java and Write a method isPalindrome that takes an IntQueue as a parameter and that returns whether or not the numbers in the queue represent a palindrome (true if they do, false otherwise). A sequence of numbers is considered a palindrome if it is the same in reverse order. For example, suppose a Queue called q stores this sequence of values: front [3, 8, 17, 9, 17, 8, 3] back Then the following call:...

  • 1. Write a static method named mode that takes an array of integers as a parameter...

    1. Write a static method named mode that takes an array of integers as a parameter and that returns the value that occurs most frequently in the array. Assume that the integers in the array appear in sorted order. For example, if a variable called list stores the following values: ist -3, 1, 4, 4, 4, 6, 7,8, 8, 8, 8, 9, 11, 11, 11, 12, 14, int 141i Then the call of mode (li array, appearing four times. st,...

  • ****WRITE A JAVA PROGRAM THAT : Write a method named stretch that accepts an array of...

    ****WRITE A JAVA PROGRAM THAT : Write a method named stretch that accepts an array of integers (that the user inputs) as a parameter and returns a new array twice as large as the original, replacing every integer from the original array with a pair of integers, each half the original. If a number in the original array is odd, then the first number in the new pair should be one higher than the second so that the sum equals...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT