In this post, We will learn How to write a Java Program to Reverse the first K elements of a Queue?
First, let’s try to understand the problem statement in little more detail for better clarity:
You have given an integer K and a Queue of Integers then How would you reverse the order of the first K elements of the Queue, leaving the other elements in the same relative order.
For Example, if K = 6 and Queue has the following elements [10,20,30,40,50,60,70,80,90,100]
Then Output should be [60,50,40,30,20,10,70,80,90,100]
ClientTest.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
package com.kkjavatutorials.util; import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; import java.util.Stack; /** * @author KK JavaTutorials * Client program to Reverse First K elements in Queue */ public class ClientTest { public static void main(String[] args) { Queue<Integer> queue = new 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); queue.add(100); System.out.println("Original Queue::"); System.out.println(queue); Scanner scanner = null; try { System.out.println("How many Elements You want to reverse from Start:"); scanner = new Scanner(System.in); int k = scanner.nextInt(); Queue<Integer> reverseQueueFirstKElements = reverseQueueFirstKElements(k,queue); System.out.println("Queue After Reversing first "+k+" Elements:"); System.out.println(reverseQueueFirstKElements); } catch (Exception e) { e.printStackTrace(); } } /** * @param k Reverse first K Elements * @param queue input * @return final Output Reversed first K Elements of input Queue */ public static Queue<Integer> reverseQueueFirstKElements(int k, Queue<Integer> queue) { //Input Validation if(queue == null || k > queue.size()) { throw new IllegalArgumentException("Input is not valid!!"); } else if(k > 0) { Stack<Integer> stack = new Stack<>(); /*Remove first K Elements from input Queue and push into Stack*/ for (int i = 0; i < k; i++) { stack.push(queue.remove()); } /** * if Stack is not Empty * then pop Item from Stack * and add into input Queue */ while(!stack.isEmpty()) { queue.add(stack.pop()); } //Wrap around rest of elements in input Queue for (int i = 0; i < queue.size()-k; i++) { queue.add(queue.remove()); } } return queue; } } |
The program has tested for below input:
Original Queue::
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
How many Elements You want to reverse from Start:
6
Queue After Reversing first 6 Elements:
[60, 50, 40, 30, 20, 10, 70, 80, 90, 100]
You May run this program to test for other input as well.
You May Also Like:
Queue Data Structure
Queue Implementation using an array in java
Queue Implementation using LinkedList in java
That’s all about Java Program to Reverse the first K elements of a Queue?
If you have any feedback or suggestion please feel free to drop in below comment box.