-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathP29.java
More file actions
44 lines (33 loc) · 1007 Bytes
/
P29.java
File metadata and controls
44 lines (33 loc) · 1007 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
package stack_and_queue;
import java.util.LinkedList;
import java.util.Queue;
// Interleave the first half of the queue with second half
public class P29 {
public static void interLeaveQueue(Queue<Integer> q) {
if (q.size() % 2 != 0)
System.out.println("Input even no of integers");
Queue<Integer> temp = new LinkedList<>();
int half_size = q.size() / 2;
for (int i = 0; i < half_size; i++) {
temp.add(q.element());
q.poll();
}
while (!temp.isEmpty()) {
q.add(temp.element());
q.add(q.element());
q.poll();
temp.poll();
}
}
public static void main(String[] args) {
Queue<Integer> Q = new LinkedList<>();
// Add numbers to end of Queue
Q.add(1);
Q.add(2);
Q.add(3);
Q.add(4);
System.out.println("Queue: " + Q);
interLeaveQueue(Q);
System.out.println("Queue: " + Q);
}
}