-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterleavingQueues.java
More file actions
44 lines (41 loc) · 882 Bytes
/
interleavingQueues.java
File metadata and controls
44 lines (41 loc) · 882 Bytes
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
import java.util.*;
public class interleavingQueues {
private static Queue<Integer> interleaveQueues(Queue<Integer> q){
if(q.isEmpty()){
return null;
}
Queue<Integer> q1 = new LinkedList<Integer>();
Queue<Integer> q2 = new LinkedList<Integer>();
int n = q.size();
int hf = n/2;
int i = 1;
while(i <= hf){
q1.add(q.remove());
i++;
}
while(i <= n){
q2.add(q.remove());
i++;
}
while(!q1.isEmpty() && !q2.isEmpty()){
q.add(q1.remove());
q.add(q2.remove());
}
return q;
}
public static void main(String args[]){
Queue<Integer> q = new LinkedList<Integer>();
q.add(11);
q.add(12);
q.add(13);
q.add(14);
q.add(15);
q.add(16);
q.add(17);
q.add(18);
q.add(19);
q.add(20);
q = interleaveQueues(q);
System.out.print(q+" ");
}
}