-
Notifications
You must be signed in to change notification settings - Fork 1
/
Queues.java
395 lines (334 loc) · 10.5 KB
/
Queues.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
import java.util.*;
public class Queues {
static class QueueArr {
static int[] arr;
static int size;
static int rear;
static int front;
//Constructor
QueueArr(int n) {
arr = new int[n];
size = n;
rear = -1;
front = -1;
}
//isEmpty Method: To check queue is Empty or NOT.
public static boolean isEmpty() {
return rear == -1 && front == -1;
}
//isFull Method: To check Circular Queue is Full or NOT.
public static boolean isFull() {
return (rear + 1) % size == front;
}
//Add (Enque) Method. O(1)
public static void add(int data) {
if (isFull()) {
System.out.println("Queue is Full.");
return;
}
//Case: while Adding First element.
if (front == -1) {
front = 0;
}
rear = (rear + 1) % size;
arr[rear] = data;
}
//Remove (Dequeue) Method. O(1)
public static int remove() {
if (isEmpty()) {
System.out.print("Queue is Empty.");
return -1;
}
int result = arr[front];
//Case: when Removing Last element.
if (rear == front) {
rear = front = -1;
} else {
front = (front + 1) % size;
}
return result;
}
//Peek O(1)
public static int peek() {
if (isEmpty()) {
System.out.print("Queue is Empty.");
return -1;
}
return arr[front];
}
}
static class Node {
int data;
Node next;
Node(int data) {
this.data = data;
this.next = null;
}
}
static class QueueLL {
static Node head = null;
static Node tail = null;
//isEmpty Method: To check queue is Empty or NOT.
public static boolean isEmpty() {
return head == null && tail == null;
}
//Add (Enque) Method. O(1)
public static void add(int data) {
Node newNode = new Node(data);
//Case: When there is NO any Node in LL.
if (head == null) {
head = tail = newNode;
return;
}
tail.next = newNode;
tail = newNode;
}
//Remove (Dequeue) Method. O(1)
public static int remove() {
if (isEmpty()) {
System.out.print("Queue is Empty.");
return -1;
}
int front = head.data;
//Case: When there is single element in LL.
if (tail == head) {
head = tail = null;
} else {
head = head.next;
}
return front;
}
//Peek O(1)
public static int peek() {
if (isEmpty()) {
System.out.print("Queue is Empty.");
return -1;
}
return head.data;
}
}
static Stack<Integer> s1 = new Stack<>();
static Stack<Integer> s2 = new Stack<>();
//isEmpty
public static boolean isEmpty() {
return s1.isEmpty();
}
//Add: O(n)
public static void add(int data) {
while (!s1.isEmpty()) {
s2.push(s1.pop());
}
s1.push(data);
while (!s2.isEmpty()) {
s1.push(s2.pop());
}
}
//Remove: O(1)
public static int remove() {
if (isEmpty()) {
System.out.println("Queue is Empty.");
return -1;
}
return s1.pop();
}
//Peek: O(1)
public static int peek() {
if (isEmpty()) {
System.out.println("Queue is Empty.");
return -1;
}
return s1.peek();
}
static class Stacks {
static Queue<Integer> q1 = new LinkedList<>();
static Queue<Integer> q2 = new LinkedList<>();
//isEmpty
public static boolean isEmpty() {
return q1.isEmpty() && q2.isEmpty();
}
//Push: O(1)
public static void push(int data) {
if (!q1.isEmpty()) {
q1.add(data);
} else {
q2.add(data);
}
}
//Pop: O(n)
public static int pop() {
if (isEmpty()) {
System.out.println("Stack is Empty.");
return -1;
}
int top = -1;
if (!q1.isEmpty()) { //Case 1
while (!q1.isEmpty()) {
top = q1.remove();
if (q1.isEmpty()) {
break;
}
q2.add(top);
}
} else { //Case 2
while (!q2.isEmpty()) {
top = q2.remove();
if (q2.isEmpty()) {
break;
}
q1.add(top);
}
}
return top;
}
//Peek: O(n)
public static int peek() {
if (isEmpty()) {
System.out.println("Stack is Empty.");
return -1;
}
int top = -1;
if (!q1.isEmpty()) { //Case 1
while (!q1.isEmpty()) {
top = q1.remove();
q2.add(top);
}
} else { //Case 2
while (!q2.isEmpty()) {
top = q2.remove();
q1.add(top);
}
}
return top;
}
}
//First non-repeating letter in a stream of a character using queues. O(n)
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.println(-1 + " ");
} else {
System.out.println(q.peek() + " ");
}
}
System.out.println();
}
//Interleave 2 Halves of a Queue (even length). TC: O(n) & SC: O(n)
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()); //Adding element in 2nd queue.
}
while (!firstHalf.isEmpty()) {
q.add(firstHalf.remove()); //Adding element from 2nd queue to main queue.
q.add(q.remove()); //Adding element from main queue to itself again.
}
}
//Queue Reversal. O(n)
public static void reverse(Queue<Integer> q) {
Stack<Integer> s = new Stack<>();
while (!q.isEmpty()) {
s.push(q.remove());
}
while (!s.isEmpty()) {
q.add(s.pop());
}
}
//Implement Stack using Deque.
static class DequeStack {
Deque<Integer> deque = new LinkedList<>();
//push: O(1)
public void push(int data) {
deque.addLast(data); //adding data from last side.
}
//pop: O(1)
public int pop() {
return deque.removeLast(); //removing data from last side.
}
//peek: O(1)
public int peek() {
return deque.getLast(); //getting data from first side.
}
}
//Implement Queue using Deque.
static class DequeQueue {
Deque<Integer> deque = new LinkedList<>();
//Add: O(1)
public void add(int data) {
deque.addLast(data); //adding data from last side.
}
//Remove: O(1)
public int remove() {
return deque.removeFirst(); //removing data from last side.
}
//Peek: O(1)
public int peek() {
return deque.getFirst(); //getting data from first side.
}
}
public static void main(String[] para_coder) {
// QueueLL q = new QueueLL(); //Here we've defined size of the array.
/*FYI: Queue is an Interface so, we cannot create object of it. So, we need any other class to implement it.
* There are two classes in Java who can implement Queue interfaces. One is Linked List and Second is ArrayDeque.*/
// Queue<Character> q = new LinkedList<>();
// Queue<Character> q = new ArrayDeque<>();
// Queues q = new Queues();
/*Stacks s = new Stacks();
s.push(1);
s.push(2);
s.push(3);
while (!s.isEmpty()) {
System.out.println(s.peek());
s.pop();
}*/
//First non-repeating letter in a stream of a character using queues. O(n)
// String str = "aabccxb";
// printNonRepeating(str);
//Interleave 2 Halves of a Queue (even length). TC: O(n) & SC: O(n)
//Queue Reversal. O(n)
/*Deque<Integer> deque = new LinkedList<>(); //Remember: Deque is an Interface, which is implemented by other classes like Liked List.
deque.addFirst(1); //1
deque.addFirst(2); //2, 1
deque.addFirst(3); //3, 2, 1
deque.addLast(3); //3, 2, 1, 3
deque.addLast(4); //3, 2, 1, 3, 4
System.out.println(deque);
deque.removeLast(); //3, 2, 1, 3
System.out.println(deque);
deque.removeFirst(); //2, 1, 3
System.out.println(deque);
System.out.println("First Element: " + deque.getFirst()); //2
System.out.println("Last Element: " + deque.getLast()); //3*/
// interLeave(q);
// reverse(q);
/*while (!q.isEmpty()) {
System.out.print(q.remove() + " ");
}*/
//Implement Stack using Deque.
/*DequeStack s = new DequeStack();
s.push(1);
s.push(2);
s.push(3);
System.out.println("Peek: " + s.peek());
System.out.println(s.pop());
System.out.println(s.pop());
System.out.println(s.pop());*/
//Implement Queue using Deque.
DequeQueue q = new DequeQueue();
q.add(1);
q.add(2);
q.add(3);
System.out.println("Peek: " + q.peek());
System.out.println(q.remove());
System.out.println(q.remove());
System.out.println(q.remove());
}
}