diff --git a/src/main/java/com/thealgorithms/datastructures/stacks/ReverseStack.java b/src/main/java/com/thealgorithms/datastructures/stacks/ReverseStack.java index e9de14b53302..d87f5f4ea86a 100644 --- a/src/main/java/com/thealgorithms/datastructures/stacks/ReverseStack.java +++ b/src/main/java/com/thealgorithms/datastructures/stacks/ReverseStack.java @@ -38,6 +38,9 @@ private ReverseStack() { * @param stack the stack to reverse; should not be null */ public static void reverseStack(Stack stack) { + if (stack == null) { + throw new IllegalArgumentException("Stack cannot be null"); + } if (stack.isEmpty()) { return; } diff --git a/src/main/java/com/thealgorithms/others/ReverseStackUsingRecursion.java b/src/main/java/com/thealgorithms/others/ReverseStackUsingRecursion.java deleted file mode 100644 index de36673512a0..000000000000 --- a/src/main/java/com/thealgorithms/others/ReverseStackUsingRecursion.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.thealgorithms.others; - -import java.util.Stack; - -/** - * Class that provides methods to reverse a stack using recursion. - */ -public final class ReverseStackUsingRecursion { - private ReverseStackUsingRecursion() { - } - - /** - * Reverses the elements of the given stack using recursion. - * - * @param stack the stack to be reversed - * @throws IllegalArgumentException if the stack is null - */ - public static void reverse(Stack stack) { - if (stack == null) { - throw new IllegalArgumentException("Stack cannot be null"); - } - if (!stack.isEmpty()) { - int topElement = stack.pop(); - reverse(stack); - insertAtBottom(stack, topElement); - } - } - - /** - * Inserts an element at the bottom of the given stack. - * - * @param stack the stack where the element will be inserted - * @param element the element to be inserted at the bottom - */ - private static void insertAtBottom(Stack stack, int element) { - if (stack.isEmpty()) { - stack.push(element); - } else { - int topElement = stack.pop(); - insertAtBottom(stack, element); - stack.push(topElement); - } - } -} diff --git a/src/test/java/com/thealgorithms/datastructures/stacks/ReverseStackTest.java b/src/test/java/com/thealgorithms/datastructures/stacks/ReverseStackTest.java index 2e2bc5adae3a..a4e781c84127 100644 --- a/src/test/java/com/thealgorithms/datastructures/stacks/ReverseStackTest.java +++ b/src/test/java/com/thealgorithms/datastructures/stacks/ReverseStackTest.java @@ -1,6 +1,7 @@ package com.thealgorithms.datastructures.stacks; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.Stack; @@ -8,6 +9,11 @@ class ReverseStackTest { + @Test + void testReverseNullStack() { + assertThrows(IllegalArgumentException.class, () -> ReverseStack.reverseStack(null), "Reversing a null stack should throw an IllegalArgumentException."); + } + @Test void testReverseEmptyStack() { Stack stack = new Stack<>(); diff --git a/src/test/java/com/thealgorithms/others/ReverseStackUsingRecursionTest.java b/src/test/java/com/thealgorithms/others/ReverseStackUsingRecursionTest.java deleted file mode 100644 index 23b99ae87d35..000000000000 --- a/src/test/java/com/thealgorithms/others/ReverseStackUsingRecursionTest.java +++ /dev/null @@ -1,58 +0,0 @@ -package com.thealgorithms.others; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import java.util.Stack; -import org.junit.jupiter.api.Test; - -public class ReverseStackUsingRecursionTest { - - @Test - void testReverseWithMultipleElements() { - Stack stack = new Stack<>(); - for (int i = 0; i < 5; i++) { - stack.push(i); - } - - ReverseStackUsingRecursion.reverse(stack); - - for (int i = 0; i < 5; i++) { - assertEquals(i, stack.pop()); - } - assertTrue(stack.isEmpty()); - } - - @Test - void testReverseWithSingleElement() { - Stack stack = new Stack<>(); - stack.push(1); - - ReverseStackUsingRecursion.reverse(stack); - - assertEquals(1, stack.pop()); - assertTrue(stack.isEmpty()); - } - - @Test - void testReverseWithEmptyStack() { - Stack stack = new Stack<>(); - - ReverseStackUsingRecursion.reverse(stack); - - assertTrue(stack.isEmpty()); - } - - @Test - void testReverseWithNullStack() { - Stack stack = null; - - Exception exception = assertThrows(IllegalArgumentException.class, () -> ReverseStackUsingRecursion.reverse(stack)); - - String expectedMessage = "Stack cannot be null"; - String actualMessage = exception.getMessage(); - - assertTrue(actualMessage.contains(expectedMessage)); - } -}