mirror of
https://github.com/TheAlgorithms/Java.git
synced 2026-03-13 08:40:43 +08:00
* 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:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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 @@ import org.junit.jupiter.api.Test;
|
||||
|
||||
class ReverseStackTest {
|
||||
|
||||
@Test
|
||||
void testReverseNullStack() {
|
||||
assertThrows(IllegalArgumentException.class, () -> ReverseStack.reverseStack(null), "Reversing a null stack should throw an IllegalArgumentException.");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testReverseEmptyStack() {
|
||||
Stack<Integer> stack = new Stack<>();
|
||||
|
||||
@@ -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<Integer> 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<Integer> stack = new Stack<>();
|
||||
stack.push(1);
|
||||
|
||||
ReverseStackUsingRecursion.reverse(stack);
|
||||
|
||||
assertEquals(1, stack.pop());
|
||||
assertTrue(stack.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testReverseWithEmptyStack() {
|
||||
Stack<Integer> stack = new Stack<>();
|
||||
|
||||
ReverseStackUsingRecursion.reverse(stack);
|
||||
|
||||
assertTrue(stack.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testReverseWithNullStack() {
|
||||
Stack<Integer> stack = null;
|
||||
|
||||
Exception exception = assertThrows(IllegalArgumentException.class, () -> ReverseStackUsingRecursion.reverse(stack));
|
||||
|
||||
String expectedMessage = "Stack cannot be null";
|
||||
String actualMessage = exception.getMessage();
|
||||
|
||||
assertTrue(actualMessage.contains(expectedMessage));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user