-
Notifications
You must be signed in to change notification settings - Fork 0
/
06_dequeue.java
60 lines (48 loc) · 1.97 KB
/
06_dequeue.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
import java.util.*;
// double ended queue
// Pronounced as "Deck"
// Allow insertion and removal from both the ends
//
class MyCollections {
public static void main(String args[]) {
example_queue();
}
public static void example_queue() {
// Create using
// Deque <Data Type> workingDaysInQueue = new List <Data Type> ();
Deque <String> workingDaysInDeque = new LinkedList<String> ();
workingDaysInDeque.add("Monday");
workingDaysInDeque.add("Tuesday");
workingDaysInDeque.add("Wednesday");
workingDaysInDeque.add("Thursday");
workingDaysInDeque.add("Friday");
// Size of the dequeue
System.out.println("\nWorkday DQ Size : " + workingDaysInDeque.size() );
// In a dequeue, elements are added at the end using regular add method
workingDaysInDeque.add("Saturday");
// In a dequeue, elements are added at the start using addFirst method
workingDaysInDeque.addFirst("xxx");
// In a dequeue, elements are added at the end using addLast method = add method
workingDaysInDeque.addLast("Sunday");
// In a dequeue, elements are removed from the start
String item = workingDaysInDeque.remove();
System.out.println("Removed item {" + item + "} from the DQ");
// In a dequeue, elements are removed from the start using poll
item = workingDaysInDeque.poll();
System.out.println("Removed item {" + item + "} from the DQ");
// In a dequeue, elements are removed from the start using pollLast
item = workingDaysInDeque.pollLast();
System.out.println("Removed item {" + item + "} from the DQ");
// Accessing elements in a queue
System.out.println("\nIterator : ");
Iterator itor = workingDaysInDeque.iterator();
while( itor.hasNext() ){
System.out.println("Element = " + itor.next() );
}
// Another way to access elements in a dequeue
System.out.println("\nforEach : ");
workingDaysInDeque.forEach( element -> {
System.out.println("Element = " + element );
});
}
}