Repository: TheAlgorithms/Java
Pull request #6018 (2024-10-26 04:27:08)

refactor: Enhance docs, remove main, add tests in `ReverseStack`
<!--
Thank you for your contribution!
In order to reduce the number of notifications sent to the maintainers, please:
- create your PR as draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->

<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [x] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/TheAlgorithms/Java/pull/6018?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 69.20%. Comparing base [(`0f1dcbe`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/0f1dcbe47930d9a30f1beaee359c183669b4d1fb?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`c12675f`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/c12675f7c010936cd020dc0d0ce3ec8a4c20dba0?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).

<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff              @@
##             master    #6018      +/-   ##
============================================
+ Coverage     69.07%   69.20%   +0.13%     
- Complexity     4665     4670       +5     
============================================
  Files           624      624              
  Lines         17166    17151      -15     
  Branches       3311     3309       -2     
============================================
+ Hits          11857    11870      +13     
+ Misses         4855     4827      -28     
  Partials        454      454              
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/6018?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).


Patch:
diff --git a/DIRECTORY.md b/DIRECTORY.md
index 31f46916..b7e9e3b8 100644
--- a/DIRECTORY.md
+++ b/DIRECTORY.md
@@ -871,6 +871,7 @@
               * [TokenBucketTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/queues/TokenBucketTest.java)
             * stacks
               * [NodeStackTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/stacks/NodeStackTest.java)
+              * [ReverseStackTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/stacks/ReverseStackTest.java)
               * [StackArrayListTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/stacks/StackArrayListTest.java)
               * [StackArrayTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/stacks/StackArrayTest.java)
               * [StackOfLinkedListTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/stacks/StackOfLinkedListTest.java)
diff --git a/src/main/java/com/thealgorithms/datastructures/stacks/ReverseStack.java b/src/main/java/com/thealgorithms/datastructures/stacks/ReverseStack.java
index 84e9df96..e9de14b5 100644
--- a/src/main/java/com/thealgorithms/datastructures/stacks/ReverseStack.java
+++ b/src/main/java/com/thealgorithms/datastructures/stacks/ReverseStack.java
@@ -1,10 +1,25 @@
 package com.thealgorithms.datastructures.stacks;
 
-import java.util.Scanner;
 import java.util.Stack;
 
 /**
- * Reversal of a stack using recursion.
+ * 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
  */
@@ -12,56 +27,48 @@ public final class ReverseStack {
     private ReverseStack() {
     }
 
-    public static void main(String[] args) {
-        try (Scanner sc = new Scanner(System.in)) {
-            System.out.println("Enter the number of elements you wish to insert in the stack");
-            int n = sc.nextInt();
-            int i;
-            Stack<Integer> stack = new Stack<Integer>();
-            System.out.println("Enter the stack elements");
-            for (i = 0; i < n; i++) {
-                stack.push(sc.nextInt());
-            }
-            reverseStack(stack);
-            System.out.println("The reversed stack is:");
-            while (!stack.isEmpty()) {
-                System.out.print(stack.peek() + ",");
-                stack.pop();
-            }
-        }
-    }
-
-    private static void reverseStack(Stack<Integer> stack) {
+    /**
+     * 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;
         }
 
-        // Store the topmost element
-        int element = stack.peek();
-        // Remove the topmost element
-        stack.pop();
-
-        // Reverse the stack for the leftover elements
+        int element = stack.pop();
         reverseStack(stack);
-
-        // Insert the topmost element to the bottom of the 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()) {
-            // When stack is empty, insert the element so it will be present at
-            // the bottom of the stack
             stack.push(element);
             return;
         }
 
-        int ele = stack.peek();
-        // Keep popping elements till stack becomes empty. Push the elements
-        // once the topmost element has moved to the bottom of the stack.
-        stack.pop();
+        int topElement = stack.pop();
         insertAtBottom(stack, element);
-
-        stack.push(ele);
+        stack.push(topElement);
     }
 }


Test patch:
diff --git a/src/test/java/com/thealgorithms/datastructures/stacks/ReverseStackTest.java b/src/test/java/com/thealgorithms/datastructures/stacks/ReverseStackTest.java
new file mode 100644
index 00000000..2e2bc5ad
--- /dev/null
+++ b/src/test/java/com/thealgorithms/datastructures/stacks/ReverseStackTest.java
@@ -0,0 +1,70 @@
+package com.thealgorithms.datastructures.stacks;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.util.Stack;
+import org.junit.jupiter.api.Test;
+
+class ReverseStackTest {
+
+    @Test
+    void testReverseEmptyStack() {
+        Stack<Integer> stack = new Stack<>();
+        ReverseStack.reverseStack(stack);
+        assertTrue(stack.isEmpty(), "Reversing an empty stack should result in an empty stack.");
+    }
+
+    @Test
+    void testReverseSingleElementStack() {
+        Stack<Integer> stack = new Stack<>();
+        stack.push(1);
+        ReverseStack.reverseStack(stack);
+        assertEquals(1, stack.peek(), "Reversing a single-element stack should have the same element on top.");
+    }
+
+    @Test
+    void testReverseTwoElementStack() {
+        Stack<Integer> stack = new Stack<>();
+        stack.push(1);
+        stack.push(2);
+        ReverseStack.reverseStack(stack);
+
+        assertEquals(1, stack.pop(), "After reversal, the stack's top element should be the first inserted element.");
+        assertEquals(2, stack.pop(), "After reversal, the stack's next element should be the second inserted element.");
+    }
+
+    @Test
+    void testReverseMultipleElementsStack() {
+        Stack<Integer> stack = new Stack<>();
+        stack.push(1);
+        stack.push(2);
+        stack.push(3);
+        stack.push(4);
+
+        ReverseStack.reverseStack(stack);
+
+        assertEquals(1, stack.pop(), "Stack order after reversal should match the initial push order.");
+        assertEquals(2, stack.pop());
+        assertEquals(3, stack.pop());
+        assertEquals(4, stack.pop());
+    }
+
+    @Test
+    void testReverseStackAndVerifySize() {
+        Stack<Integer> stack = new Stack<>();
+        stack.push(10);
+        stack.push(20);
+        stack.push(30);
+        stack.push(40);
+        int originalSize = stack.size();
+
+        ReverseStack.reverseStack(stack);
+
+        assertEquals(originalSize, stack.size(), "Stack size should remain unchanged after reversal.");
+        assertEquals(10, stack.pop(), "Reversal should place the first inserted element on top.");
+        assertEquals(20, stack.pop());
+        assertEquals(30, stack.pop());
+        assertEquals(40, stack.pop());
+    }
+}