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

  • 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 program with a function named isEqualArr that accepts a pair of arrays of integers...

    Write a program with a function named isEqualArr that accepts a pair of arrays of integers as parameters and returns true if the arrays contain the same elements in the same order. If the arrays are not the same length, your function should return false. For example, if the following arrays are declared: int[] arr1 = {10, 20, 30, 40, 50, 60}; int[] arr2 = {10, 20, 30, 40, 50, 60}; int[] arr3 = {20, 3, 50, 10, 68}; The...

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

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

  • Write a method called stutter that accepts an ArrayList of strings and an integer k as...

    Write a method called stutter that accepts an ArrayList of strings and an integer k as parameters and that replaces every string with k copies of that string. For example, if the list stores the values ["how", "are", "you?"] before the method is called and k is 4, it should store the values ["how", "how", "how", "how", "are", "are", "are", "are", "you?", "you?", "you?", "you?"] after the method finishes executing. If k is 0 or negative, the list should be...

  • Write a method for the Queue class in the queue.java program (Listing 4.4) that displays the...

    Write a method for the Queue class in the queue.java program (Listing 4.4) that displays the contents of the queue. Note that this does not mean simply displaying the contents of the underlying array. You should show the queue contents from the first item inserted to the last, without indicating to the viewer whether the sequence is broken by wrapping around the end of the array. Be careful that one item and no items display properly, no matter where front...

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

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