|
| 1 | +Internal Working of Stack in Java (Enhanced & Detailed) |
| 2 | + |
| 3 | +* In Java, Stack is a legacy class from the java.util package. |
| 4 | +* It extends Vector, which means: |
| 5 | +o Backed by a dynamic array (like Vector). |
| 6 | +o Inherits synchronization for thread safety. |
| 7 | + |
| 8 | +* Represents a Last-In, First-Out (LIFO) data structure. |
| 9 | +* Although still used, it is often replaced with ArrayDeque in modern applications due to performance advantages. |
| 10 | + |
| 11 | +1. Underlying Data Structure |
| 12 | + |
| 13 | +* Stack is built on top of Vector, which internally uses an array (Object[]) to store elements. |
| 14 | +* Key points: |
| 15 | +o The array resizes dynamically when capacity is exceeded. |
| 16 | +o Synchronization makes operations thread-safe but slower. |
| 17 | + |
| 18 | +Declaration: |
| 19 | +Stack<Integer> stack = new Stack<>(); |
| 20 | + |
| 21 | +Internally: |
| 22 | +Vector<Object> elementData = new Vector<>(initialCapacity); |
| 23 | +* Default capacity starts at 10. |
| 24 | +* Resizing: when the array is full, capacity is doubled. |
| 25 | + |
| 26 | +2. Core Operations of Stack |
| 27 | + |
| 28 | +push(E item) |
| 29 | +* Adds element to the top of the stack. |
| 30 | +* Internally calls addElement(item) from Vector. |
| 31 | + |
| 32 | +Example: |
| 33 | +stack.push(10); // adds 10 to stack top |
| 34 | + |
| 35 | +Flow: |
| 36 | +1. Check capacity. |
| 37 | +2. If full ? grow array. |
| 38 | +3. Place element at elementData[size]. |
| 39 | +4. Increment size. |
| 40 | + |
| 41 | +pop() |
| 42 | +* Removes and returns the top element. |
| 43 | +* Internally: |
| 44 | +o Retrieves last element via elementAt(size-1). |
| 45 | +o Calls removeElementAt(size-1). |
| 46 | + |
| 47 | +Example: |
| 48 | +int x = stack.pop(); |
| 49 | + |
| 50 | +Flow: |
| 51 | +1. Call peek() to get element. |
| 52 | +2. Reduce size by 1. |
| 53 | +3. Nullify the removed slot for GC. |
| 54 | + |
| 55 | +peek() |
| 56 | +* Returns the top element without removing it. |
| 57 | +* Uses elementAt(size-1). |
| 58 | +* Throws EmptyStackException if empty. |
| 59 | + |
| 60 | +empty() |
| 61 | +* Checks if the stack has no elements. |
| 62 | +* Returns size() == 0. |
| 63 | + |
| 64 | +search(Object o) |
| 65 | +* Returns the 1-based position from the top of the stack. |
| 66 | +* If not found, returns -1. |
| 67 | +* Internally scans linearly (O(n)). |
| 68 | + |
| 69 | +Complexity of Operations |
| 70 | + |
| 71 | +* push(E) ? O(1) amortized (resizing may cost O(n)). |
| 72 | +* pop() ? O(1). |
| 73 | +* peek() ? O(1). |
| 74 | +* empty() ? O(1). |
| 75 | +* search() ? O(n) (linear search). |
| 76 | + |
| 77 | + |
| 78 | +Code |
| 79 | + |
| 80 | +Class Declaration |
| 81 | +public class Stack<E> extends Vector<E> { |
| 82 | + public Stack() { } |
| 83 | +} |
| 84 | + |
| 85 | +push() |
| 86 | +public E push(E item) { |
| 87 | + addElement(item); // Vector method |
| 88 | + return item; |
| 89 | +} |
| 90 | + |
| 91 | +pop() |
| 92 | +public synchronized E pop() { |
| 93 | + E obj; |
| 94 | + int len = size(); |
| 95 | + obj = peek(); |
| 96 | + removeElementAt(len - 1); |
| 97 | + return obj; |
| 98 | +} |
| 99 | + |
| 100 | +peek() |
| 101 | +public synchronized E peek() { |
| 102 | + int len = size(); |
| 103 | + if (len == 0) |
| 104 | + throw new EmptyStackException(); |
| 105 | + return elementAt(len - 1); |
| 106 | +} |
| 107 | + |
| 108 | +Example Code |
| 109 | + |
| 110 | +import java.util.*; |
| 111 | + |
| 112 | +public class StackDemo { |
| 113 | + public static void main(String[] args) { |
| 114 | + Stack<Integer> stack = new Stack<>(); |
| 115 | + |
| 116 | + stack.push(10); |
| 117 | + stack.push(20); |
| 118 | + stack.push(30); |
| 119 | + |
| 120 | + System.out.println("Stack: " + stack); // [10, 20, 30] |
| 121 | + System.out.println("Top: " + stack.peek()); // 30 |
| 122 | + |
| 123 | + stack.pop(); |
| 124 | + System.out.println("After pop: " + stack); // [10, 20] |
| 125 | + |
| 126 | + System.out.println("Search 10: " + stack.search(10)); // 2 |
| 127 | + System.out.println("Is empty? " + stack.empty()); // false |
| 128 | + } |
| 129 | +} |
0 commit comments