Skip to content

Commit 647a82a

Browse files
authored
Enhance docs, remove main, add tests in ReverseStack (#6018)
1 parent b0cef5b commit 647a82a

File tree

3 files changed

+116
-38
lines changed

3 files changed

+116
-38
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,7 @@
871871
* [TokenBucketTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/queues/TokenBucketTest.java)
872872
* stacks
873873
* [NodeStackTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/stacks/NodeStackTest.java)
874+
* [ReverseStackTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/stacks/ReverseStackTest.java)
874875
* [StackArrayListTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/stacks/StackArrayListTest.java)
875876
* [StackArrayTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/stacks/StackArrayTest.java)
876877
* [StackOfLinkedListTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/stacks/StackOfLinkedListTest.java)
Lines changed: 45 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,74 @@
11
package com.thealgorithms.datastructures.stacks;
22

3-
import java.util.Scanner;
43
import java.util.Stack;
54

65
/**
7-
* Reversal of a stack using recursion.
6+
* Provides methods to reverse a stack using recursion.
7+
*
8+
* <p>This class includes methods to reverse the order of elements in a stack
9+
* without using additional data structures. Elements are inserted at the bottom
10+
* of the stack to achieve the reverse order.
11+
*
12+
* <p>Example usage:
13+
* <pre>
14+
* Stack<Integer> stack = new Stack<>();
15+
* stack.push(1);
16+
* stack.push(2);
17+
* stack.push(3);
18+
* ReverseStack.reverseStack(stack);
19+
* </pre>
20+
* After calling {@code reverseStack(stack)}, the stack's order is reversed.
21+
*
22+
* <p>This class is final and has a private constructor to prevent instantiation.
823
*
924
* @author Ishika Agarwal, 2021
1025
*/
1126
public final class ReverseStack {
1227
private ReverseStack() {
1328
}
1429

15-
public static void main(String[] args) {
16-
try (Scanner sc = new Scanner(System.in)) {
17-
System.out.println("Enter the number of elements you wish to insert in the stack");
18-
int n = sc.nextInt();
19-
int i;
20-
Stack<Integer> stack = new Stack<Integer>();
21-
System.out.println("Enter the stack elements");
22-
for (i = 0; i < n; i++) {
23-
stack.push(sc.nextInt());
24-
}
25-
reverseStack(stack);
26-
System.out.println("The reversed stack is:");
27-
while (!stack.isEmpty()) {
28-
System.out.print(stack.peek() + ",");
29-
stack.pop();
30-
}
31-
}
32-
}
33-
34-
private static void reverseStack(Stack<Integer> stack) {
30+
/**
31+
* Reverses the order of elements in the given stack using recursion.
32+
* Steps:
33+
* 1. Check if the stack is empty. If so, return.
34+
* 2. Pop the top element from the stack.
35+
* 3. Recursively reverse the remaining stack.
36+
* 4. Insert the originally popped element at the bottom of the reversed stack.
37+
*
38+
* @param stack the stack to reverse; should not be null
39+
*/
40+
public static void reverseStack(Stack<Integer> stack) {
3541
if (stack.isEmpty()) {
3642
return;
3743
}
3844

39-
// Store the topmost element
40-
int element = stack.peek();
41-
// Remove the topmost element
42-
stack.pop();
43-
44-
// Reverse the stack for the leftover elements
45+
int element = stack.pop();
4546
reverseStack(stack);
46-
47-
// Insert the topmost element to the bottom of the stack
4847
insertAtBottom(stack, element);
4948
}
5049

50+
/**
51+
* Inserts the specified element at the bottom of the stack.
52+
*
53+
* <p>This method is a helper for {@link #reverseStack(Stack)}.
54+
*
55+
* Steps:
56+
* 1. If the stack is empty, push the element and return.
57+
* 2. Remove the top element from the stack.
58+
* 3. Recursively insert the new element at the bottom of the stack.
59+
* 4. Push the removed element back onto the stack.
60+
*
61+
* @param stack the stack in which to insert the element; should not be null
62+
* @param element the element to insert at the bottom of the stack
63+
*/
5164
private static void insertAtBottom(Stack<Integer> stack, int element) {
5265
if (stack.isEmpty()) {
53-
// When stack is empty, insert the element so it will be present at
54-
// the bottom of the stack
5566
stack.push(element);
5667
return;
5768
}
5869

59-
int ele = stack.peek();
60-
// Keep popping elements till stack becomes empty. Push the elements
61-
// once the topmost element has moved to the bottom of the stack.
62-
stack.pop();
70+
int topElement = stack.pop();
6371
insertAtBottom(stack, element);
64-
65-
stack.push(ele);
72+
stack.push(topElement);
6673
}
6774
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.thealgorithms.datastructures.stacks;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
6+
import java.util.Stack;
7+
import org.junit.jupiter.api.Test;
8+
9+
class ReverseStackTest {
10+
11+
@Test
12+
void testReverseEmptyStack() {
13+
Stack<Integer> stack = new Stack<>();
14+
ReverseStack.reverseStack(stack);
15+
assertTrue(stack.isEmpty(), "Reversing an empty stack should result in an empty stack.");
16+
}
17+
18+
@Test
19+
void testReverseSingleElementStack() {
20+
Stack<Integer> stack = new Stack<>();
21+
stack.push(1);
22+
ReverseStack.reverseStack(stack);
23+
assertEquals(1, stack.peek(), "Reversing a single-element stack should have the same element on top.");
24+
}
25+
26+
@Test
27+
void testReverseTwoElementStack() {
28+
Stack<Integer> stack = new Stack<>();
29+
stack.push(1);
30+
stack.push(2);
31+
ReverseStack.reverseStack(stack);
32+
33+
assertEquals(1, stack.pop(), "After reversal, the stack's top element should be the first inserted element.");
34+
assertEquals(2, stack.pop(), "After reversal, the stack's next element should be the second inserted element.");
35+
}
36+
37+
@Test
38+
void testReverseMultipleElementsStack() {
39+
Stack<Integer> stack = new Stack<>();
40+
stack.push(1);
41+
stack.push(2);
42+
stack.push(3);
43+
stack.push(4);
44+
45+
ReverseStack.reverseStack(stack);
46+
47+
assertEquals(1, stack.pop(), "Stack order after reversal should match the initial push order.");
48+
assertEquals(2, stack.pop());
49+
assertEquals(3, stack.pop());
50+
assertEquals(4, stack.pop());
51+
}
52+
53+
@Test
54+
void testReverseStackAndVerifySize() {
55+
Stack<Integer> stack = new Stack<>();
56+
stack.push(10);
57+
stack.push(20);
58+
stack.push(30);
59+
stack.push(40);
60+
int originalSize = stack.size();
61+
62+
ReverseStack.reverseStack(stack);
63+
64+
assertEquals(originalSize, stack.size(), "Stack size should remain unchanged after reversal.");
65+
assertEquals(10, stack.pop(), "Reversal should place the first inserted element on top.");
66+
assertEquals(20, stack.pop());
67+
assertEquals(30, stack.pop());
68+
assertEquals(40, stack.pop());
69+
}
70+
}

0 commit comments

Comments
 (0)