-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDequeInterface.java
More file actions
166 lines (149 loc) · 6.11 KB
/
Copy pathDequeInterface.java
File metadata and controls
166 lines (149 loc) · 6.11 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package Phase5_CollectionsLambdasStreams.Collections;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.LinkedList;
/**
* java.util.Deque<E> - "Double-Ended Queue"
* -----------------------------------------
* A Deque (pronounced "deck") is a queue that supports insert and remove
* at BOTH ENDS. It can be used as a queue, a stack, or both.
* <p>
*
* addFirst -> [A][B][C][D] <- addLast
* pollFirst <- [A][B][C][D] -> pollLast
* <p>
*
* The Three "Personalities" of a Deque
* ------------------------------------
* QUEUE (FIFO): offer / poll (same as Queue interface)
* STACK (LIFO): push / pop (push = addFirst, pop = removeFirst)
* DEQUE (both): addFirst / addLast / pollFirst / pollLast / peek...
* <p>
*
* The same Deque object can do all three at the same time.
* <p>
*
* Method Cheatsheet
* -----------------
* <p>
*
* Throws on failure Returns special value
* ---------------------- ----------------------
* addFirst(e) / addLast(e) offerFirst(e) / offerLast(e)
* removeFirst() / removeLast() pollFirst() / pollLast()
* getFirst() / getLast() peekFirst() / peekLast()
* <p>
*
* Stack flavour:
* push(e) -> addFirst(e)
* pop() -> removeFirst()
* peek() -> peekFirst()
* <p>
*
* Implementations
* ---------------
* ArrayDeque - resizing circular array. NO nulls. The DEFAULT.
* Faster than LinkedList for queue and stack duties.
* LinkedList - doubly-linked list. Allows nulls. Slower in benchmarks
* but is also a List.
* ConcurrentLinkedDeque, LinkedBlockingDeque - concurrent variants.
* <p>
*
* Why You Should Prefer ArrayDeque For Stacks
* -------------------------------------------
* The legacy java.util.Stack class extends Vector (synchronized). It is
* slow and discouraged. The modern recommendation in the JDK Javadoc:
* <p>
*
* "Deque interface and its implementations provide a more complete
* and consistent set of LIFO stack operations, which should be used
* in preference to this class."
* <p>
*
* The Big-O
* ---------
* addFirst / addLast / removeFirst / removeLast / peekFirst / peekLast
* O(1)
* contains / remove(Object) O(n)
*/
public class DequeInterface {
public static void main(String[] args) {
section("1) Queue (FIFO) usage");
Deque<String> q = new ArrayDeque<>();
q.offerLast("a"); // queue tail
q.offerLast("b");
q.offerLast("c");
System.out.println("queue = " + q);
System.out.println("poll = " + q.pollFirst() + " -> " + q);
section("2) Stack (LIFO) usage");
Deque<Integer> stack = new ArrayDeque<>();
stack.push(1);
stack.push(2);
stack.push(3);
System.out.println("stack (top first) = " + stack); // [3, 2, 1]
System.out.println("peek = " + stack.peek());
System.out.println("pop = " + stack.pop() + " -> " + stack);
section("3) Deque (both ends)");
Deque<Integer> d = new ArrayDeque<>();
d.addFirst(2);
d.addFirst(1);
d.addLast(3);
d.addLast(4);
System.out.println("deque = " + d);
System.out.println("peekFirst = " + d.peekFirst());
System.out.println("peekLast = " + d.peekLast());
System.out.println("removeFirst -> " + d.removeFirst() + " deque=" + d);
System.out.println("removeLast -> " + d.removeLast() + " deque=" + d);
section("4) Throws vs returns-special");
Deque<Integer> empty = new ArrayDeque<>();
System.out.println("pollFirst on empty = " + empty.pollFirst()); // null
System.out.println("peekFirst on empty = " + empty.peekFirst()); // null
try {
empty.getFirst();
} catch (java.util.NoSuchElementException e) {
System.out.println("getFirst on empty -> NoSuchElementException");
}
section("5) ArrayDeque vs LinkedList - same Deque API");
Deque<Integer> ad = new ArrayDeque<>();
Deque<Integer> ll = new LinkedList<>();
for (int n : new int[]{1, 2, 3}) { ad.push(n); ll.push(n); }
System.out.println("ArrayDeque stack = " + ad);
System.out.println("LinkedList stack = " + ll);
// ArrayDeque is recommended - smaller and faster, just doesn't allow null.
section("6) ArrayDeque rejects null (a deliberate safety feature)");
try { ad.offer(null); }
catch (NullPointerException e) {
System.out.println("ArrayDeque.offer(null) -> NullPointerException");
}
// LinkedList allows null - but that ambiguity is why ArrayDeque forbids it.
section("7) Real-world example - undo / redo stacks");
Deque<String> undo = new ArrayDeque<>();
Deque<String> redo = new ArrayDeque<>();
undo.push("typed 'h'");
undo.push("typed 'i'");
undo.push("typed '!'");
System.out.println("history = " + undo);
// user hits ctrl-z twice
redo.push(undo.pop());
redo.push(undo.pop());
System.out.println("after 2 undos: history = " + undo + ", redo = " + redo);
// user hits ctrl-y once
undo.push(redo.pop());
System.out.println("after 1 redo : history = " + undo + ", redo = " + redo);
section("8) descendingIterator - walk tail to head");
Deque<Integer> walk = new ArrayDeque<>();
for (int n = 1; n <= 5; n++) walk.offerLast(n);
System.out.print("forward : ");
for (Integer n : walk) System.out.print(n + " ");
System.out.println();
System.out.print("backward: ");
for (java.util.Iterator<Integer> it = walk.descendingIterator(); it.hasNext(); ) {
System.out.print(it.next() + " ");
}
System.out.println();
// OUTPUT (representative)
}
private static void section(String title) {
System.out.println("\n====== " + title + " ======");
}
}