forked from IamBisrutPyne/Java-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrees.java
More file actions
29 lines (22 loc) · 878 Bytes
/
trees.java
File metadata and controls
29 lines (22 loc) · 878 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import java.util.Stack;
public class trees {
public static void main(String[] args) {
// Create a stack of integers
Stack<Integer> stack = new Stack<>();
// Push elements (add to stack)
stack.push(10);
stack.push(20);
stack.push(30);
stack.push(40);
System.out.println("Stack after pushes: " + stack);
// Peek (see top element without removing)
System.out.println("Top element (peek): " + stack.peek());
// Pop (remove top element)
System.out.println("Popped element: " + stack.pop());
System.out.println("Stack after pop: " + stack);
// Search (find position from top, 1-based)
System.out.println("Position of 20 from top: " + stack.search(20));
// Check if empty
System.out.println("Is stack empty? " + stack.isEmpty());
}
}