-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertBottomOfStack.java
More file actions
37 lines (29 loc) · 1.09 KB
/
Copy pathInsertBottomOfStack.java
File metadata and controls
37 lines (29 loc) · 1.09 KB
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
import java.util.Stack;
public class InsertBottomOfStack {
public static void main(String[] args) {
// Create a stack and add some elements
Stack<Integer> st = new Stack<>();
st.push(1);
st.push(2);
st.push(3);
st.push(4);
System.out.println("Original Stack: " + st);
// Specify the index and value to insert
int index = 2; // Index where the new element will be inserted
int x = 89; // Value to be inserted
// Create a temporary stack to hold elements
Stack<Integer> temp = new Stack<>();
// Move elements above the specified index to the temporary stack
while (st.size() > index) {
temp.push(st.pop());
}
// Push the new element at the specified index
st.push(x);
// Restore the original elements from the temporary stack
while (temp.size() > 0) {
st.push(temp.pop());
}
// Output the modified stack
System.out.println("Modified Stack: " + st);
}
}