-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLearnQueue.java
More file actions
43 lines (39 loc) · 1.17 KB
/
LearnQueue.java
File metadata and controls
43 lines (39 loc) · 1.17 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
import java.util.LinkedList;
import java.util.Queue;
public class LearnQueue {
public static void main(String[] args) {
// This is how we initialize queue
Queue<Integer> q = new LinkedList<>();
Queue<Integer> q2 = new LinkedList<>();
// This is how we add elements in queue
q.add(1);
q.add(1);
q.add(2);
q.add(2);
q.add(3);
q.add(3);
q.add(4);
q.add(4);
q.add(5);
q.add(6);
System.out.println(q);
System.out.println(q2);
// System.out.println(q.poll());
// System.out.println(q);
// We use size() method to find out the size of the queue.
// We use peek() method to find out the first element of the queue.
// We use poll() method to remove the first element of the queue.
for(int i = 0; i<q.size();i++){
if(q2.peek()==q.peek()){
q.poll();
continue;
}
else
{
q2.add(q.peek());
q.poll();
}
}
System.out.println(q2);
}
}