-
Notifications
You must be signed in to change notification settings - Fork 19.9k
/
Copy pathReverseStack.java
74 lines (68 loc) · 2.26 KB
/
ReverseStack.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
package com.thealgorithms.datastructures.stacks;
import java.util.Stack;
/**
* Provides methods to reverse a stack using recursion.
*
* <p>This class includes methods to reverse the order of elements in a stack
* without using additional data structures. Elements are inserted at the bottom
* of the stack to achieve the reverse order.
*
* <p>Example usage:
* <pre>
* Stack<Integer> stack = new Stack<>();
* stack.push(1);
* stack.push(2);
* stack.push(3);
* ReverseStack.reverseStack(stack);
* </pre>
* After calling {@code reverseStack(stack)}, the stack's order is reversed.
*
* <p>This class is final and has a private constructor to prevent instantiation.
*
* @author Ishika Agarwal, 2021
*/
public final class ReverseStack {
private ReverseStack() {
}
/**
* Reverses the order of elements in the given stack using recursion.
* Steps:
* 1. Check if the stack is empty. If so, return.
* 2. Pop the top element from the stack.
* 3. Recursively reverse the remaining stack.
* 4. Insert the originally popped element at the bottom of the reversed stack.
*
* @param stack the stack to reverse; should not be null
*/
public static void reverseStack(Stack<Integer> stack) {
if (stack.isEmpty()) {
return;
}
int element = stack.pop();
reverseStack(stack);
insertAtBottom(stack, element);
}
/**
* Inserts the specified element at the bottom of the stack.
*
* <p>This method is a helper for {@link #reverseStack(Stack)}.
*
* Steps:
* 1. If the stack is empty, push the element and return.
* 2. Remove the top element from the stack.
* 3. Recursively insert the new element at the bottom of the stack.
* 4. Push the removed element back onto the stack.
*
* @param stack the stack in which to insert the element; should not be null
* @param element the element to insert at the bottom of the stack
*/
private static void insertAtBottom(Stack<Integer> stack, int element) {
if (stack.isEmpty()) {
stack.push(element);
return;
}
int topElement = stack.pop();
insertAtBottom(stack, element);
stack.push(topElement);
}
}