Skip to content

Commit 7d7c0f5

Browse files
committed
Solving new question to reverse a Queue
1 parent 009ff95 commit 7d7c0f5

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

Queu/Question.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
package Java.Queu;
22
import java.util.*;
33
public class Question {
4+
public static void reverse(Queue<Integer> q){
5+
Stack <Integer> s = new Stack<>();
6+
7+
while(!q.isEmpty()){
8+
s.push(q.remove());
9+
}
10+
while(!s.isEmpty()){
11+
q.add(s.pop());
12+
}
13+
}
414
public static void printNonRepating(String str){
515
int freq [] = new int[26];
616
Queue<Character> q = new LinkedList<>();
@@ -25,5 +35,19 @@ public static void printNonRepating(String str){
2535
public static void main(String args[]){
2636
String str = "aabccxb";
2737
printNonRepating(str);
38+
39+
Queue<Integer> q = new LinkedList<>();
40+
q.add(1);
41+
q.add(2);
42+
q.add(3);
43+
q.add(4);
44+
q.add(5);
45+
46+
reverse(q);
47+
while (!q.isEmpty()) {
48+
System.out.print(q.remove()+ " ");
49+
}
50+
System.out.println();
51+
2852
}
2953
}

0 commit comments

Comments
 (0)