fix: revert ReverseStack deletion and verify linting (#6474) (#6494)

* fix: revert ReverseStack deletions and ensure lint compliance

* Fix formatting for ReverseStack and test files

* Delete ReverseStackUsingRecursion and its test as requested

---------

Co-authored-by: Deniz Altunkapan <93663085+DenizAltunkapan@users.noreply.github.com>
This commit is contained in:
Vicky
2025-08-22 11:11:36 +05:30
committed by GitHub
parent 3f195c2c89
commit 3961b1dab9
4 changed files with 9 additions and 102 deletions

View File

@@ -38,6 +38,9 @@ public final class ReverseStack {
* @param stack the stack to reverse; should not be null
*/
public static void reverseStack(Stack<Integer> stack) {
if (stack == null) {
throw new IllegalArgumentException("Stack cannot be null");
}
if (stack.isEmpty()) {
return;
}

View File

@@ -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<Integer> 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<Integer> stack, int element) {
if (stack.isEmpty()) {
stack.push(element);
} else {
int topElement = stack.pop();
insertAtBottom(stack, element);
stack.push(topElement);
}
}
}