Skip to content

Commit 1546b15

Browse files
committed
fix: revert ReverseStack deletions and ensure lint compliance
1 parent 0ff2c06 commit 1546b15

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

src/main/java/com/thealgorithms/datastructures/stacks/ReverseStack.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,19 @@ private ReverseStack() {
3737
*
3838
* @param stack the stack to reverse; should not be null
3939
*/
40-
public static void reverseStack(Stack<Integer> stack) {
41-
if (stack.isEmpty()) {
42-
return;
43-
}
44-
45-
int element = stack.pop();
46-
reverseStack(stack);
47-
insertAtBottom(stack, element);
40+
public static void reverseStack(Stack<Integer> stack) {
41+
if (stack == null) {
42+
throw new IllegalArgumentException("Stack cannot be null");
4843
}
44+
if (stack.isEmpty()) {
45+
return;
46+
}
47+
48+
int element = stack.pop();
49+
reverseStack(stack);
50+
insertAtBottom(stack, element);
51+
}
52+
4953

5054
/**
5155
* Inserts the specified element at the bottom of the stack.

src/test/java/com/thealgorithms/datastructures/stacks/ReverseStackTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
package com.thealgorithms.datastructures.stacks;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
45
import static org.junit.jupiter.api.Assertions.assertTrue;
56

67
import java.util.Stack;
78
import org.junit.jupiter.api.Test;
89

910
class ReverseStackTest {
1011

12+
@Test
13+
void testReverseNullStack() {
14+
assertThrows(IllegalArgumentException.class,
15+
() -> ReverseStack.reverseStack(null),
16+
"Reversing a null stack should throw an IllegalArgumentException.");
17+
}
18+
19+
1120
@Test
1221
void testReverseEmptyStack() {
1322
Stack<Integer> stack = new Stack<>();

0 commit comments

Comments
 (0)