-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStackExample.java
75 lines (54 loc) · 1.96 KB
/
StackExample.java
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package DataStructures;
import java.util.Stack;
/**
<pre>
STACK
————
LIFO (Last In, First Out)
We have Stack class & support indices.
Stack<TreeNode> stack = new Stack<>();
stack.push(ele) —> Adds an element to the top / last of the stack.
.pop() —> Removes and returns the top / last element of the stack. EmptyStackException if the stack is empty.
.peek() —> Returns the top element of the stack without removing it. EmptyStackException if the stack is empty.
.get(i) —> get(0) returns bottom/first ele. And get(s.size()-1) or peek() returns top/last ele.
.isEmpty() —> true if the stack is empty, false otherwise
search(Object o) —> Searches for an element in the stack and returns its 1-based position from the top. Returns -1 if the element is not found. Same as findIndex.
.size()
.clear()
.contains(ele) —> true if the element is found, false otherwise
.toString()
Note:
1. Use isEmpty() or try-catch or Deque to avoid the EmptyStackException when stack.pop().
2. Use Deque for both Stack and Queue alternative.
Deque<Integer> deque = new ArrayDeque<>()
</pre>
*
* @author Srinivas Vadige, srinivas.vadige@gmail.com
* @since 11 Jan 2025
*/
public class StackExample {
public static void main(String[] args) {
Stack<Integer> stack = new Stack<>();
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
stack.push(5);
System.out.println(stack.peek());
System.out.println(stack.search(3));
for (int i = 0; i < stack.size(); i++) {
System.out.println(stack.get(i));
}
for (Integer i : stack) {
System.out.println(i);
}
while (!stack.isEmpty()) {
System.out.println(stack.pop());
}
/*
for (Integer i : stack)
System.out.println(stack.pop());
*/
// but we cannot loop with i and pop() simultaneously --> It throws ConcurrentModificationException
}
}