Skip to content

Commit 861a4d6

Browse files
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 changed

Section25CollectionFramework/src/ListDemo/SatckDemo/StackDemo.java renamed to Section 25 Collections Frameworks/List Interface/Vector/Stack/src/StackDemo.java

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,8 @@
1-
package ListDemo.SatckDemo;
2-
31
import java.util.Stack;
42

53
public class StackDemo {
64
public static void main(String[] args) {
75
Stack<Integer> stack = new Stack<>();
8-
/*
9-
LIFO- Last-In First Out Principle.
10-
Last item added to the stack.That item removed first.
11-
12-
Stack extends vector
13-
Stack extends the vector. it is synchronized, making it thread safe.
14-
*/
156

167
stack.push(1);
178
stack.push(2);
@@ -33,23 +24,15 @@ public static void main(String[] args) {
3324
stack.add(2, 7);
3425
System.out.println(stack);
3526

36-
int search = stack.search(4); //getting index of Top element. from top of the stack.
37-
System.out.println(search);
38-
/*
39-
4
40-
3
41-
2
42-
1
43-
0
44-
...... Indexing of elements in stack.
27+
int search = stack.search(4);
28+
//getting index of a Top element. from the top of the stack.
4529

30+
System.out.println(search);
31+
/* 4 3 2 1 0... Indexing of elements in a stack.
4632
Returns the 1-based position where an object is on this stack.
47-
4833
If the object o occurs as an item in this stack, this method returns the distance
4934
from the top of the stack of the occurrence nearest the top of the stack;
50-
5135
the topmost item on the stack is considered to be at distance 1.
52-
5336
The equals method is used to compare o to the items in this stack.
5437
*/
5538
}

0 commit comments

Comments
 (0)