-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDequeDemo.java
More file actions
65 lines (51 loc) · 2.82 KB
/
DequeDemo.java
File metadata and controls
65 lines (51 loc) · 2.82 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package learnCollections;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.LinkedList;
public class DequeDemo {
public static void main(String[] args) {
/*
INSERTION METHODS
addFirst(E e) - adds element at the front of the deque, throws exception if deque is full
offerFirst(E e) - adds element at the front of the deque, returns false if deque is full
addLast(E e) - adds element at the end of the deque, throws exception if deque is full
offerLast(E e) - adds element at the end of the deque, returns false if deque is full
*/
/*
REMOVAL METHODS
removeFirst() - removes and returns the first element of the deque, throws exception if deque is empty
pollFirst() - removes and returns the first element of the deque, returns null if deque is empty
removeLast() - removes and returns the last element of the deque, throws exception if deque is empty
pollLast() - removes and returns the last element of the deque, returns null if deque is empty
*/
/*
EXAMINATION METHODS
getFirst() - retrieves, but does not remove, the first element of the deque, throws exception if deque is empty
peekFirst() - retrieves, but does not remove, the first element of the deque, returns null if deque is empty
getLast() - retrieves, but does not remove, the last element of the deque, throws exception if deque is empty
peekLast() - retrieves, but does not remove, the last element of the deque, returns null if deque is empty
*/
/*
STACK METHODS (Deque can be used as a stack)
push(E e) - Adds an element at the front (equivalent to addFirst(E e)). Throws exception if deque is full
pop() - Removes and returns the first element (equivalent to removeFirst()), throws exception if deque is empty
*/
Deque<Integer> deque1 = new ArrayDeque<>(); // faster iteration, low memory, no null allowed
// circular array, head and tail pointers
// no need to shift elements, just shift head and tail pointers
deque1.addFirst(10);
deque1.addLast(20);
deque1.offerFirst(5);
deque1.offerLast(25);
System.out.println(deque1); // [5, 10, 20, 25]
System.out.println("First element: " + deque1.getFirst()); // 5
System.out.println("Last element: " + deque1.getLast()); // 25
deque1.removeFirst(); // removes 5
deque1.pollLast(); // removes 25
// Current Deque: [10, 20]
for(int num: deque1) {
System.out.println(num); // 10, 20
}
Deque<Integer> deque2 = new LinkedList<>(); // insertion, deletion somewhere in middle is faster, allows null elements
}
}