-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueueG.java
More file actions
84 lines (71 loc) · 1.98 KB
/
Copy pathQueueG.java
File metadata and controls
84 lines (71 loc) · 1.98 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import java.util.*;
import java.util.LinkedList;
public class QueueG {
public static void printNonRepeating(String str) {
int freq[] = new int[26]; // 'a'-'z'
Queue<Character> q = new LinkedList<>();
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
q.add(ch);
freq[ch - 'a']++;
while (!q.isEmpty() && freq[q.peek() - 'a'] > 1) {
q.remove();
}
if (q.isEmpty()) {
System.out.print(-1 + " ");
} else {
System.out.print(q.peek() + " ");
}
}
}
public static void interLeave(Queue<Integer> q) {
Queue<Integer> firstHalf = new LinkedList<>();
int size = q.size();
for (int i = 0; i < size / 2; i++) {
firstHalf.add(q.remove());
}
while (!firstHalf.isEmpty()) {
q.add(firstHalf.remove());
q.add(q.remove());
}
}
public static void reverse(Queue<Integer> q) {
Stack<Integer> s = new Stack<>();
while (!q.isEmpty()) {
s.add(q.remove());
}
while (!s.isEmpty()) {
q.add(s.pop());
}
}
public static void main(String args[]) {
// String str = "aabccxb";
// printNonRepeating(str);
// Queue<Integer> q = new LinkedList<>();
// q.add(1);
// q.add(2);
// q.add(3);
// q.add(4);
// q.add(5);
// q.add(6);
// q.add(7);
// q.add(8);
// q.add(9);
// q.add(10);
// interLeave(q);
// // print q
// while (!q.isEmpty()) {
// System.out.print(q.remove() + " ");
// }
Queue<Integer> q1 = new LinkedList<>();
q1.add(2);
q1.add(1);
q1.add(3);
q1.add(4);
q1.add(5);
reverse(q1);
while (!q1.isEmpty()) {
System.out.print(q1.remove() + " ");
}
}
}