Question

14. 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 g stores 10, 20 30, 40, 50, 60, 70, 80, 901, the call of reverseFirstK 4, q) should change the queue to store [40, 30 20, 10, 50, 60, 70, 80, 90 1. If k is o or negative, no change should If be made. the queue does not contain at least k elements, your method should throw an IllegalArgumentException. Use one queue or stack (but not both) as auxiliary storage.

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

import java.util.Queue;

public class ReverseKelements {

   static void QueueUtil(Queue<Integer> s, int item) {

  

       s.add(item);

      

          

   }

   static void UtilQueue(Queue<Integer> q, int temp) {

       if (q.isEmpty()) {

          

           return;

       }

       int t = q.poll();

       UtilQueue(q, t);

      

       QueueUtil(q, t);

   }

   public static void reverseFirstK(Queue<Integer> queue, int k) {

       if(k<=0 )

           return;

      

       if(k>queue.size())

           throw new IllegalArgumentException();

      

       Queue<Integer> pq = new java.util.LinkedList<Integer>();

       for(int i=0;i<k;++i){

           pq.add(queue.poll());

       }

      

       UtilQueue(queue, queue.peek());

       while(!pq.isEmpty()){

           queue.add(pq.poll());

       }

      

       UtilQueue(queue, queue.peek());

      

   }

   public static void main(String[] args) {

       // TODO Auto-generated method stub

      

      

      

       Queue<Integer> queue = new java.util.LinkedList<Integer>();

       queue.add(10);

       queue.add(20);

       queue.add(30);

       queue.add(40);

       queue.add(50);

       queue.add(60);

       queue.add(70);

       queue.add(80);

       queue.add(90);

      

       System.out.println("Queue Before: ");

       System.out.println(queue.toString());

       reverseFirstK(queue,4);

       System.out.println("Queue After Reverse: ");

       System.out.println(queue.toString());

      

   }

}
==================================================
See Output

D D Stack-Java for(int 1k;++1)1 Stacklnterface M stack-Java d!!胤Problems Search 리 Console ×「Progress . 32 <terminated> Revers
Thanks

Add a comment
Know the answer?
Add Answer to:
Write a method called reverseFirstK that accepts an integer k and a queue of integers as...
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
  • 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...

  • In Java Write a method factorial that accepts an integer parameter n and that uses recursion...

    In Java Write a method factorial that accepts an integer parameter n and that uses recursion to compute and return the value of n factorial (also known as n!). Your method should throw an IllegalArgumentException if n is negative. Several calls and their return values are shown below. Call Output factorial(0); 1 factorial(1); 1 factorial(3); 6 factorial(5); 120 factorial(10); 3628800 factorial(-4); IllegalArgumentException

  • Need the answer in Java, thank you! 2. (20 points) Write a method that accepts a...

    Need the answer in Java, thank you! 2. (20 points) Write a method that accepts a temperature in Fahrheit as a parameter and returns that same temperature in Celsius. For example, if your method were called with the parameter 212, it would not print anything but would return 100. Name CSCI 161-(01.02.03)-Homework 02-Page 2 3. (20 points) Write a main method that prints a table of the Celsius equivalents of every 5tlh Fahrenheit temperature between 20 and 90. Assume that...

  • 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...

  • 1. Write a function named “areFirstTwoTheSameAsTheLastTwo” that accepts an array of integers, its size and return...

    1. Write a function named “areFirstTwoTheSameAsTheLastTwo” that accepts an array of integers, its size and return true if the first two numbers in the array are the same as the last two numbers in the array. It returns false otherwise. If the array has less than 4 elements, it always returns false. For example, this array of {10, 20, 30, 40, 10, 20} or {10, 20, 10, 20} will return true but this array of {10, 20, 20, 10} or...

  • Write a method named printReverse() that accepts an int array as its parameter and prints that...

    Write a method named printReverse() that accepts an int array as its parameter and prints that array in reverse order. This method should not have a return statement (void). - All elements should be printed on the same line, separated by a space - A new line should be printed after the entire array prints - If the array passed in held the values: {3,2,1} - Output: 1 2 3 //newline printed here

  • Create in C# Write a method called MaximumDiffrence that accepts an integer array as a parameter...

    Create in C# Write a method called MaximumDiffrence that accepts an integer array as a parameter and return the maximum difference between adjacent values in the array, where the gap is defined as the absolute value of the difference between the 2 adjacent values. Example: if the array contains {5, 7, 4, 9, 6, 12, 8} so The first gap is (5,7)=-2 and the absolute value is 2 The second gap is (7,4)=3 Third gap is (4,9) = -5 its...

  • Using C++ Insert elements into a partially filled array. a) Create a partially filled array of...

    Using C++ Insert elements into a partially filled array. a) Create a partially filled array of CAPACITY 100. b) Initialize the array with values 10,20,30,40,50,60,70,80,90,100 c) Create a print function for the array. d) Create an insert function which inserts an integer into the array in sorted position. e) Insert the numbers 5, 150 and 55. f) Print the array before and after each insertion. Output Example 10 20 30 40 50 60 70 80 90 100 5 10 20...

  • Please match the compound to its spectrum and explain why cpd 22 Spectrum A 100- 60...

    Please match the compound to its spectrum and explain why cpd 22 Spectrum A 100- 60 40 20 100 125 50 75 m/z Spectrum B 100 80- 60 40 20- 10 20 30 40 50 60 70 0 90 100 110 120 m/z (6 points) Match EOT Spectrum C 100- 80- 60 40 75 100 125 50 m/z Spectrum D 100 80 60- 40 20- 25 50 75 100 125 m/z Spectrum E 100 80 60 20 10 20 30...

  • Write a method in the HashIntSet class called addAll that accepts another hash set as a...

    Write a method in the HashIntSet class called addAll that accepts another hash set as a parameter and adds all of the elements from the other set into the current set. For example, if the set stores [-5, 1, 2, 3] and the method is passed [2, 3, 6, 44, 79], your set would store [-5, 1, 2, 3, 6, 44, 79]. Write a method in the HashIntSet class called containsAll that accepts another hash set as a parameter and...

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
Active Questions
ADVERTISEMENT