From 1546b1521f4a8e785078ce4022eab26f9e6711bc Mon Sep 17 00:00:00 2001 From: VickySource Date: Thu, 21 Aug 2025 19:41:40 +0530 Subject: [PATCH 1/3] fix: revert ReverseStack deletions and ensure lint compliance --- .../datastructures/stacks/ReverseStack.java | 20 +++++++++++-------- .../stacks/ReverseStackTest.java | 9 +++++++++ 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/stacks/ReverseStack.java b/src/main/java/com/thealgorithms/datastructures/stacks/ReverseStack.java index e9de14b53302..2d291404d8e1 100644 --- a/src/main/java/com/thealgorithms/datastructures/stacks/ReverseStack.java +++ b/src/main/java/com/thealgorithms/datastructures/stacks/ReverseStack.java @@ -37,15 +37,19 @@ private ReverseStack() { * * @param stack the stack to reverse; should not be null */ - public static void reverseStack(Stack stack) { - if (stack.isEmpty()) { - return; - } - - int element = stack.pop(); - reverseStack(stack); - insertAtBottom(stack, element); +public static void reverseStack(Stack stack) { + if (stack == null) { + throw new IllegalArgumentException("Stack cannot be null"); } + if (stack.isEmpty()) { + return; + } + + int element = stack.pop(); + reverseStack(stack); + insertAtBottom(stack, element); +} + /** * Inserts the specified element at the bottom of the stack. diff --git a/src/test/java/com/thealgorithms/datastructures/stacks/ReverseStackTest.java b/src/test/java/com/thealgorithms/datastructures/stacks/ReverseStackTest.java index 2e2bc5adae3a..6eb7e40e2d89 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,14 @@ 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<>(); From 4aa527e72e2c7c92dc9519f4f49c040706ad83af Mon Sep 17 00:00:00 2001 From: VickySource Date: Thu, 21 Aug 2025 22:50:10 +0530 Subject: [PATCH 2/3] Fix formatting for ReverseStack and test files --- .../datastructures/stacks/ReverseStack.java | 23 +++++++++---------- .../stacks/ReverseStackTest.java | 9 +++----- 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/stacks/ReverseStack.java b/src/main/java/com/thealgorithms/datastructures/stacks/ReverseStack.java index 2d291404d8e1..d87f5f4ea86a 100644 --- a/src/main/java/com/thealgorithms/datastructures/stacks/ReverseStack.java +++ b/src/main/java/com/thealgorithms/datastructures/stacks/ReverseStack.java @@ -37,19 +37,18 @@ 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; - } - - int element = stack.pop(); - reverseStack(stack); - insertAtBottom(stack, element); -} + public static void reverseStack(Stack stack) { + if (stack == null) { + throw new IllegalArgumentException("Stack cannot be null"); + } + if (stack.isEmpty()) { + return; + } + int element = stack.pop(); + reverseStack(stack); + insertAtBottom(stack, element); + } /** * Inserts the specified element at the bottom of the stack. diff --git a/src/test/java/com/thealgorithms/datastructures/stacks/ReverseStackTest.java b/src/test/java/com/thealgorithms/datastructures/stacks/ReverseStackTest.java index 6eb7e40e2d89..a4e781c84127 100644 --- a/src/test/java/com/thealgorithms/datastructures/stacks/ReverseStackTest.java +++ b/src/test/java/com/thealgorithms/datastructures/stacks/ReverseStackTest.java @@ -10,12 +10,9 @@ class ReverseStackTest { @Test -void testReverseNullStack() { - assertThrows(IllegalArgumentException.class, - () -> ReverseStack.reverseStack(null), - "Reversing a null stack should throw an IllegalArgumentException."); -} - + void testReverseNullStack() { + assertThrows(IllegalArgumentException.class, () -> ReverseStack.reverseStack(null), "Reversing a null stack should throw an IllegalArgumentException."); + } @Test void testReverseEmptyStack() { From c13bf5129ac4c6955f98201c9e618d558d4336de Mon Sep 17 00:00:00 2001 From: VickySource Date: Fri, 22 Aug 2025 09:12:11 +0530 Subject: [PATCH 3/3] Delete ReverseStackUsingRecursion and its test as requested --- .../others/ReverseStackUsingRecursion.java | 44 -------------- .../ReverseStackUsingRecursionTest.java | 58 ------------------- 2 files changed, 102 deletions(-) delete mode 100644 src/main/java/com/thealgorithms/others/ReverseStackUsingRecursion.java delete mode 100644 src/test/java/com/thealgorithms/others/ReverseStackUsingRecursionTest.java 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/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)); - } -}