Skip to content

Commit 0b0b26e

Browse files
authored
refactor: ReverseStackUsingRecursion (TheAlgorithms#5386)
1 parent e1d8b6f commit 0b0b26e

File tree

2 files changed

+85
-46
lines changed

2 files changed

+85
-46
lines changed
Lines changed: 27 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,44 @@
11
package com.thealgorithms.others;
22

3-
/* Program to reverse a Stack using Recursion*/
43
import java.util.Stack;
54

5+
/**
6+
* Class that provides methods to reverse a stack using recursion.
7+
*/
68
public final class ReverseStackUsingRecursion {
79
private ReverseStackUsingRecursion() {
810
}
911

10-
// Stack
11-
private static Stack<Integer> stack = new Stack<>();
12-
13-
// Main function
14-
public static void main(String[] args) {
15-
// To Create a Dummy Stack containing integers from 0-9
16-
for (int i = 0; i < 10; i++) {
17-
stack.push(i);
18-
}
19-
System.out.println("STACK");
20-
21-
// To print that dummy Stack
22-
for (int k = 9; k >= 0; k--) {
23-
System.out.println(k);
12+
/**
13+
* Reverses the elements of the given stack using recursion.
14+
*
15+
* @param stack the stack to be reversed
16+
* @throws IllegalArgumentException if the stack is null
17+
*/
18+
public static void reverse(Stack<Integer> stack) {
19+
if (stack == null) {
20+
throw new IllegalArgumentException("Stack cannot be null");
2421
}
25-
26-
// Reverse Function called
27-
reverseUsingRecursion(stack);
28-
29-
System.out.println("REVERSED STACK : ");
30-
// To print reversed stack
31-
while (!stack.isEmpty()) {
32-
System.out.println(stack.pop());
22+
if (!stack.isEmpty()) {
23+
int topElement = stack.pop();
24+
reverse(stack);
25+
insertAtBottom(stack, topElement);
3326
}
3427
}
3528

36-
// Function Used to reverse Stack Using Recursion
37-
private static void reverseUsingRecursion(Stack<Integer> stack) {
38-
if (stack.isEmpty()) { // If stack is empty then return
39-
return;
40-
}
41-
/* All items are stored in call stack until we reach the end*/
42-
43-
int temptop = stack.peek();
44-
stack.pop();
45-
reverseUsingRecursion(stack); // Recursion call
46-
insertAtEnd(temptop); // Insert items held in call stack one by one into stack
47-
}
48-
49-
// Function used to insert element at the end of stack
50-
private static void insertAtEnd(int temptop) {
29+
/**
30+
* Inserts an element at the bottom of the given stack.
31+
*
32+
* @param stack the stack where the element will be inserted
33+
* @param element the element to be inserted at the bottom
34+
*/
35+
private static void insertAtBottom(Stack<Integer> stack, int element) {
5136
if (stack.isEmpty()) {
52-
stack.push(temptop); // If stack is empty push the element
37+
stack.push(element);
5338
} else {
54-
int temp = stack.peek();
55-
/* All the items are stored in call stack until we reach end*/
56-
stack.pop();
57-
58-
insertAtEnd(temptop); // Recursive call
59-
60-
stack.push(temp);
39+
int topElement = stack.pop();
40+
insertAtBottom(stack, element);
41+
stack.push(topElement);
6142
}
6243
}
6344
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.thealgorithms.others;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
7+
import java.util.Stack;
8+
import org.junit.jupiter.api.Test;
9+
10+
public class ReverseStackUsingRecursionTest {
11+
12+
@Test
13+
void testReverseWithMultipleElements() {
14+
Stack<Integer> stack = new Stack<>();
15+
for (int i = 0; i < 5; i++) {
16+
stack.push(i);
17+
}
18+
19+
ReverseStackUsingRecursion.reverse(stack);
20+
21+
for (int i = 0; i < 5; i++) {
22+
assertEquals(i, stack.pop());
23+
}
24+
assertTrue(stack.isEmpty());
25+
}
26+
27+
@Test
28+
void testReverseWithSingleElement() {
29+
Stack<Integer> stack = new Stack<>();
30+
stack.push(1);
31+
32+
ReverseStackUsingRecursion.reverse(stack);
33+
34+
assertEquals(1, stack.pop());
35+
assertTrue(stack.isEmpty());
36+
}
37+
38+
@Test
39+
void testReverseWithEmptyStack() {
40+
Stack<Integer> stack = new Stack<>();
41+
42+
ReverseStackUsingRecursion.reverse(stack);
43+
44+
assertTrue(stack.isEmpty());
45+
}
46+
47+
@Test
48+
void testReverseWithNullStack() {
49+
Stack<Integer> stack = null;
50+
51+
Exception exception = assertThrows(IllegalArgumentException.class, () -> ReverseStackUsingRecursion.reverse(stack));
52+
53+
String expectedMessage = "Stack cannot be null";
54+
String actualMessage = exception.getMessage();
55+
56+
assertTrue(actualMessage.contains(expectedMessage));
57+
}
58+
}

0 commit comments

Comments
 (0)