@@ -83,43 +83,65 @@ public class FamilyGenericTest {
8383** Q:** In ` List<? super Parent> family ` , why can't you read the result as ` Parent ` ?
8484** A:** Because the list could actually be ` List<Grandparent> ` , and reading a Grandparent as Parent would be unsafe downcasting.
8585
86- ## 🃏 Deque Stack vs Queue Operations
86+ ## 🃏 Deque: Stack vs Queue Operations
8787
88- ** Rule:** Deque can act as both ** Stack (LIFO)** and ** Queue (FIFO)** with different method behaviors.
89- - ** Stack operations** : ` push() ` and ` pop() ` work at the ** front/head** (LIFO - Last In First Out).
90- - ** Queue operations** : ` offer()/add() ` at ** tail** , ` poll()/remove() ` at ** head** (FIFO - First In First Out).
91- - ** Mixed usage** can cause confusion - know which end each method operates on.
88+ ** Rule:** Deque can act as both ** Stack (LIFO)** and ** Queue (FIFO)** - you can mix operations but understand which end each method operates on.
89+
90+ ### Core Structures:
91+ - ** Queue (FIFO)** : Add to tail, remove from head - ` offer()/add() ` and ` poll()/remove() `
92+ - ** Stack (LIFO)** : Add and remove from head - ` push() ` and ` pop() `
93+
94+ ### All Available Methods:
95+ - ** Queue methods** : ` offer(e) ` , ` add(e) ` (add to tail), ` poll() ` , ` remove() ` (remove from head)
96+ - ** Stack methods** : ` push(e) ` (add to head), ` pop() ` (remove from head)
97+ - ** Deque-specific** : ` addFirst(e) ` , ` addLast(e) ` , ` removeFirst() ` , ` removeLast() `
98+ - ** Flexible** : ` offerFirst(e) ` , ` offerLast(e) ` , ` pollFirst() ` , ` pollLast() `
99+ - ** Peek methods** : ` peek() ` , ` peekFirst() ` , ` peekLast() ` , ` element() ` (look without removing)
100+
101+ ### Key Differences:
102+ - ** Exception vs null** : ` add()/remove() ` throw exceptions when fail, ` offer()/poll() ` return false/null
103+ - ** Same operations** : ` poll() ` = ` pollFirst() ` , ` add() ` = ` addLast() ` , ` offer() ` = ` offerLast() `
92104
93105``` java
94- public class FamilyLineup {
106+ public class FamilyDequeDemo {
95107 public static void main (String [] args ) {
96- Deque<String > familyLine = new ArrayDeque<> ();
108+ Deque<String > family = new ArrayDeque<> ();
97109
98- // Using Stack operations (all work at FRONT/HEAD )
99- familyLine . push(" Father " ); // [Father]
100- familyLine . push(" Mother " ); // [Mother, Father] - Mother at front
101- familyLine . push(" Child " ); // [Child, Mother, Father] - Child at front
110+ // Stack operations (LIFO )
111+ family . push(" Grandpa " ); // [Grandpa] (head)
112+ family . push(" Dad " ); // [Dad, Grandpa] (Dad at head)
113+ family . push(" Son " ); // [Son, Dad, Grandpa] (Son at head)
102114
103- // Mixed operations - be careful!
104- System . out. println(familyLine. pollFirst()); // Child (removes from front/head)
105- System . out. println(familyLine. poll()); // Mother (poll() = pollFirst(), removes from front/head)
106- System . out. println(familyLine. pollLast()); // Father (removes from back/tail)
115+ // Queue operations mixed with stack
116+ family. offerLast(" Aunt" ); // [Son, Dad, Grandpa, Aunt] (Aunt at tail)
117+ family. addFirst(" Uncle" ); // [Uncle, Son, Dad, Grandpa, Aunt] (Uncle at head)
107118
108- // Output:
109- // Child
110- // Mother
111- // Father
119+ // Various removal methods
120+ System . out. println(family. pop()); // Uncle (stack: remove from head)
121+ System . out. println(family. pollFirst()); // Son (queue: remove from head)
122+ System . out. println(family. poll()); // Dad (same as pollFirst())
123+ System . out. println(family. removeLast()); // Aunt (deque: remove from tail)
124+ System . out. println(family. pollLast()); // Grandpa (remove from tail)
125+
126+ // Peek operations (non-destructive)
127+ family. offer(" Mom" );
128+ family. push(" Baby" ); // [Baby, Mom]
129+ System . out. println(family. peek()); // Baby (peek at head)
130+ System . out. println(family. peekLast()); // Mom (peek at tail)
131+ System . out. println(family. element()); // Baby (same as peek, but throws if empty)
132+
133+ // Output: Uncle, Son, Dad, Aunt, Grandpa, Baby, Mom, Baby
112134 }
113135}
114-
115- // Stack view: [Child, Mother, Father] (Child is top/front)
116- // Queue view: [Child, Mother, Father] (Child is head, Father is tail)
117136```
118137
119- ** 💡 Learning Tip:** Remember "STACK FRONT, QUEUE ENDS" - Stack operations (push/pop) work at front only, Queue operations work at opposite ends (add tail, remove head).
138+ ** 💡 Learning Tips:**
139+ - ** Method naming logic** : ` offer ` /` poll ` = Queue style, ` push ` /` pop ` = Stack style, ` First ` /` Last ` = specify end
140+ - ** You can mix operations** : Use stack methods and queue methods on the same Deque
141+ - ** Exception handling** : Methods ending in exception (` remove ` , ` add ` , ` element ` ) vs safe (` poll ` , ` offer ` , ` peek ` )
120142
121- ** Q:** If you push three elements then call pollFirst(), poll(), and pollLast(), what's the removal order ?
122- ** A:** First element pushed, second element pushed, third element pushed - because pollFirst() and poll() both remove from head, pollLast() from tail .
143+ ** Q:** What happens when you ` push() ` then ` poll() ` ?
144+ ** A:** Both operate on the head, so ` poll() ` removes what ` push() ` just added - like using it as a stack .
123145
124146## 🃏 Set Operations and Characteristics
125147
0 commit comments