-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReverseStack.java
More file actions
39 lines (30 loc) · 1.07 KB
/
ReverseStack.java
File metadata and controls
39 lines (30 loc) · 1.07 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
38
39
import java.util.Stack;
public class ReverseStack {
public static void reverseStack(Stack<Integer> stack) {
if (stack.isEmpty()) {
return; // Nothing to reverse if the stack is empty
}
Stack<Integer> auxStack = new Stack<>();
// Pop elements from the original stack and push them onto the auxiliary stack
while (!stack.isEmpty()) {
int item = stack.pop();
auxStack.push(item);
}
// Now, the auxiliary stack contains the elements in reverse order
// Pop elements from the auxiliary stack and push them back into the original stack
while (!auxStack.isEmpty()) {
int item = auxStack.pop();
stack.push(item);
}
}
public static void main(String[] args) {
Stack<Integer> stack = new Stack<>();
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
System.out.println("Original stack: " + stack);
reverseStack(stack);
System.out.println("Reversed stack: " + stack);
}
}