DynamicStack stack; stack.push(10); stack.push(20); stack.push(30); stack.pop(); stack.push(40); stack.print();
After pushing 10, 20, 30, the stack is 10 -> 20 -> 30 -> null.
Pop removes 30, so stack is 10 -> 20 -> null.
Push 40 adds it on top: 10 -> 20 -> 40 -> null.
Starting capacity is 2.
Push 3rd element: capacity doubles to 4.
Push 5th element: capacity doubles again to 8.
int pop() { if (size == 0) { return arr[size--]; } return arr[--size]; }
If size is 0, arr[size--] accesses arr[0] then decrements size to -1.
Next pop or access will cause invalid index access.
DynamicStack stack; stack.push(1); stack.push(2); stack.push(3); stack.push(4); stack.pop(); stack.pop(); stack.push(5); stack.print();
Push 1,2,3,4: stack is 1 -> 2 -> 3 -> 4 -> null.
Pop twice removes 4 and 3: stack is 1 -> 2 -> null.
Push 5: stack is 1 -> 2 -> 5 -> null.
Dynamic stacks typically double capacity when full to reduce resizing frequency.
They also reduce capacity by half when usage drops below 25% to save memory.
