-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue Reversal.java
More file actions
65 lines (54 loc) · 1.83 KB
/
Queue Reversal.java
File metadata and controls
65 lines (54 loc) · 1.83 KB
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
//{ Driver Code Starts
import java.util.*;
import java.io.*;
class Reversing{
public static void main(String[] args)throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
//Reading total number of testcases
int t=Integer.parseInt(br.readLine());
while(t-->0){
//Reading total number of elements
int n=Integer.parseInt(br.readLine());
//Creating a Queue
Queue<Integer> q=new LinkedList<>();
//Reading all the elements in a string
String s=br.readLine();
//Spliting the string into different
//string separated by space
String[] a=s.split(" ");
//adding all the elements to the Queue
for(String b:a){
int x=Integer.parseInt(b);
q.add(x);
}
//Creating an object of class Geeks
GfG g=new GfG();
//calling rev method of class Geeks
q=g.rev(q);
//printing the elements of the queue
while(!q.isEmpty()){
int x=q.peek();
q.poll();
System.out.print(x+" ");
}
System.out.println();
}
}
}
// } Driver Code Ends
//User function Template for Java
/*Complete the function below*/
class GfG{
//Function to reverse the queue.
public Queue<Integer> rev(Queue<Integer> q){
//add code here.
Queue<Integer> qrev = new LinkedList<Integer>();
int lastIndex=q.size()-1;
Integer[] arr = q.toArray(new Integer[q.size()]);
for(int i=lastIndex;i>=0;i--)
{
qrev.add(arr[i]);
}
return qrev;
}
}