Commit 861a4d6
committed
feat(StackDemo): add demo of Stack operations (push, pop, peek, search)
What
- Added StackDemo class.
- Created a Stack<Integer>.
- Demonstrated common stack operations:
- push(E) → added [1,2,3,4,5].
- pop() → removed and returned top element (5).
- peek() → viewed top element without removing.
- isEmpty() → checked if stack is empty.
- size() → retrieved number of elements.
- add(index,E) → inserted element 7 at index 2 (showing Stack also supports List methods).
- search(Object) → found 1-based position of element 4 from the top of stack.
- Printed intermediate and final results.
Why
- Illustrates standard LIFO (Last-In-First-Out) stack behavior.
- Shows additional functionality inherited from Vector (size, add at index).
- Demonstrates how search() differs from normal index-based lookups.
How
- stack.push(1..5) → stack = [1,2,3,4,5].
- stack.pop() → removes 5, returns 5, stack = [1,2,3,4].
- stack.peek() → returns 4, stack unchanged.
- stack.isEmpty() → false.
- stack.size() → 4.
- stack.add(2,7) → stack = [1,2,7,3,4].
- stack.search(4) → top = 4 (distance = 1).
Logic
- Inputs: integers pushed onto stack.
- Outputs:
- Printed stack contents after push, pop, and add.
- Printed removed element, top element, emptiness, size, and search position.
- Flow:
1. Push integers 1–5.
2. Print stack.
3. Pop top element → print updated stack.
4. Peek → print top element.
5. Check emptiness and size.
6. Add element 7 at index 2.
7. Search for element 4 → print position.
- Edge cases:
- pop() or peek() on empty stack → EmptyStackException.
- search() returns -1 if element not found.
- Complexity:
- push, pop, peek O(1).
- search, add(index) O(n).
- Concurrency: Stack is synchronized (extends Vector).
Real-life applications
- Undo/redo functionality.
- Expression evaluation (postfix/infix parsing).
- Function call stack simulations.
- Depth-first search algorithms.
Notes
- Stack is a legacy class; modern code often prefers Deque (e.g., ArrayDeque) for stack behavior.
- search() result is 1-based, not zero-based, which is easy to misinterpret.
- add(index,E) shows Stack’s List heritage but is rarely used in stack-specific logic.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent aa45ddd commit 861a4d6
File tree
1 file changed
+4
-21
lines changed- Section 25 Collections Frameworks/List Interface/Vector/Stack/src
1 file changed
+4
-21
lines changedLines changed: 4 additions & 21 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | 1 | | |
4 | 2 | | |
5 | 3 | | |
6 | 4 | | |
7 | 5 | | |
8 | | - | |
9 | | - | |
10 | | - | |
11 | | - | |
12 | | - | |
13 | | - | |
14 | | - | |
15 | 6 | | |
16 | 7 | | |
17 | 8 | | |
| |||
33 | 24 | | |
34 | 25 | | |
35 | 26 | | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | | - | |
41 | | - | |
42 | | - | |
43 | | - | |
44 | | - | |
| 27 | + | |
| 28 | + | |
45 | 29 | | |
| 30 | + | |
| 31 | + | |
46 | 32 | | |
47 | | - | |
48 | 33 | | |
49 | 34 | | |
50 | | - | |
51 | 35 | | |
52 | | - | |
53 | 36 | | |
54 | 37 | | |
55 | 38 | | |
| |||
0 commit comments