|
| 1 | +LIFO - Last-In First Out Principle. Last item added to the stack. That item removed first. |
| 2 | + |
| 3 | +Stack extends the vector. it is synchronized, making it thread safe. |
| 4 | + |
| 5 | +Internal Working of Stack in Java: |
| 6 | + |
| 7 | +Introduction |
| 8 | +------------ |
| 9 | +- In Java, `Stack` is a class in the `java.util` package. |
| 10 | +- It extends `Vector`, which means: |
| 11 | + - It is backed by a **dynamic array** (just like Vector). |
| 12 | + - It inherits synchronization (thread-safety). |
| 13 | +- `Stack` represents a **Last-In, First-Out (LIFO)** data structure. |
| 14 | + |
| 15 | +--- |
| 16 | + |
| 17 | +1. Underlying Data Structure |
| 18 | +---------------------------- |
| 19 | +- Stack is built on **Vector**, which uses a **dynamic array** internally. |
| 20 | +- Because of this: |
| 21 | + - Size grows dynamically when needed. |
| 22 | + - Operations are synchronized (slower than ArrayDeque). |
| 23 | + |
| 24 | +Declaration: |
| 25 | +Stack<Integer> stack = new Stack<>(); |
| 26 | + |
| 27 | +Internally: |
| 28 | +Vector<Object> elementData = new Vector<>(initialCapacity); |
| 29 | + |
| 30 | +--- |
| 31 | + |
| 32 | +2. Core Operations of Stack |
| 33 | +--------------------------- |
| 34 | + |
| 35 | +1. push(E item) |
| 36 | + - Adds (inserts) an element to the top of the stack. |
| 37 | + - Internally, it calls `addElement(item)` from Vector. |
| 38 | + |
| 39 | + Example: |
| 40 | + stack.push(10); // adds 10 to stack top |
| 41 | + |
| 42 | +2. pop() |
| 43 | + - Removes and returns the top element. |
| 44 | + - Internally: |
| 45 | + - Retrieves last element via `elementAt(size-1)`. |
| 46 | + - Then calls `removeElementAt(size-1)`. |
| 47 | + |
| 48 | + Example: |
| 49 | + int x = stack.pop(); // removes top element |
| 50 | + |
| 51 | +3. peek() |
| 52 | + - Returns the top element **without removing it**. |
| 53 | + - Internally uses `elementAt(size-1)`. |
| 54 | + |
| 55 | +4. empty() |
| 56 | + - Returns `true` if stack is empty. |
| 57 | + |
| 58 | +5. search(Object o) |
| 59 | + - Returns the 1-based position from the top of the stack. |
| 60 | + - If not found, returns -1. |
| 61 | + |
| 62 | +--- |
| 63 | + |
| 64 | +3. ASCII Representation |
| 65 | +----------------------- |
| 66 | + |
| 67 | +Empty Stack: |
| 68 | +[] |
| 69 | +size = 0, capacity = 10 |
| 70 | + |
| 71 | +After stack.push(5), stack.push(10), stack.push(20): |
| 72 | +[5, 10, 20] |
| 73 | +size = 3, capacity = 10 |
| 74 | +Top → 20 |
| 75 | + |
| 76 | +After stack.pop(): |
| 77 | +[5, 10] |
| 78 | +size = 2 |
| 79 | +Top → 10 |
| 80 | + |
| 81 | +--- |
| 82 | + |
| 83 | +4. Complexity of Operations |
| 84 | +---------------------------- |
| 85 | +- push(E) → O(1) amortized (resizing may cost O(n)) |
| 86 | +- pop() → O(1) |
| 87 | +- peek() → O(1) |
| 88 | +- search() → O(n) (linear search in array) |
| 89 | + |
| 90 | +--- |
| 91 | + |
| 92 | +5. Advantages of Stack |
| 93 | +----------------------- |
| 94 | +- Simple and direct LIFO implementation. |
| 95 | +- Thread-safe (because it extends Vector). |
| 96 | +- Useful for recursion problems, expression evaluation, undo/redo, etc. |
| 97 | + |
| 98 | +--- |
| 99 | + |
| 100 | +6. Limitations of Stack |
| 101 | +------------------------ |
| 102 | +- Synchronization makes it **slower** than alternatives. |
| 103 | +- For most cases, **ArrayDeque** is recommended because: |
| 104 | + - Faster (non-synchronized). |
| 105 | + - Provides stack (push/pop) and queue operations. |
| 106 | + |
| 107 | +--- |
| 108 | + |
| 109 | +7. Example Code |
| 110 | +--------------- |
| 111 | + |
| 112 | +import java.util.*; |
| 113 | + |
| 114 | +public class StackDemo { |
| 115 | + public static void main(String[] args) { |
| 116 | + Stack<Integer> stack = new Stack<>(); |
| 117 | + |
| 118 | + stack.push(10); |
| 119 | + stack.push(20); |
| 120 | + stack.push(30); |
| 121 | + |
| 122 | + System.out.println("Stack: " + stack); // [10, 20, 30] |
| 123 | + System.out.println("Top: " + stack.peek()); // 30 |
| 124 | + |
| 125 | + stack.pop(); |
| 126 | + System.out.println("After pop: " + stack); // [10, 20] |
| 127 | + |
| 128 | + System.out.println("Search 10: " + stack.search(10)); // 2 |
| 129 | + System.out.println("Is empty? " + stack.empty()); // false |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +--- |
| 134 | + |
| 135 | +Summary |
| 136 | +---------- |
| 137 | +- Stack in Java is a **synchronized, LIFO** data structure. |
| 138 | +- Internally backed by **Vector (dynamic array)**. |
| 139 | +- Operations: |
| 140 | + - push() → add element |
| 141 | + - pop() → remove top |
| 142 | + - peek() → check top |
| 143 | +- For performance-critical apps, prefer **ArrayDeque** instead of Stack. |
0 commit comments