Skip to content

Commit 54e98dd

Browse files
authored
Create Queue-Reverse Queue
1 parent 926f784 commit 54e98dd

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

Queue-Reverse Queue

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
You have been given a queue that can store integers as the data. You are required to write a function that reverses the populated queue itself without using any other data structures.
2+
Example:
3+
Alt txt
4+
5+
Alt txt
6+
7+
Input Format:
8+
The first list of input contains an integer 't' denoting the number of test cases/queries to be run.
9+
Then the test cases follow.
10+
11+
The first line input for each test case/query contains an integer N, denoting the total number of elements in the queue.
12+
13+
The second line of input contains N integers separated by a single space, representing the order in which the elements are enqueued into the queue.
14+
Output Format:
15+
For each test case/query, the only line of output prints the order in which the queue elements are dequeued, all of them separated by a single space.
16+
17+
Output for every test case/query will be printed on a new line.
18+
Note:
19+
You are not required to print the expected output explicitly, it has already been taken care of. Just make the changes in the input queue itself.
20+
Constraints:
21+
1 <= t <= 100
22+
1 <= N <= 10^4
23+
-2^31 <= data <= 2^31 - 1
24+
25+
Time Limit: 1sec
26+
Sample Input 1:
27+
1
28+
6
29+
1 2 3 4 5 10
30+
Note:
31+
Here, 1 is at the front and 10 is at the rear of the queue.
32+
Sample Output 1:
33+
10 5 4 3 2 1
34+
Sample Input 2:
35+
2
36+
5
37+
2 8 15 1 10
38+
3
39+
10 20 30
40+
Sample Output 2:
41+
10 1 15 8 2
42+
30 20 10
43+
44+
**************************************************Code***********************************************
45+
46+
import java.util.LinkedList;
47+
import java.util.Queue;
48+
49+
public class Solution {
50+
51+
public static void reverseQueue(Queue<Integer> input) {
52+
53+
if(input.size()==0 || input.size()==1)
54+
return;
55+
int temp=input.remove();
56+
reverseQueue(input);
57+
input.add(temp);
58+
}
59+
60+
}

0 commit comments

Comments
 (0)