|
1 | 1 | package com.thealgorithms.others;
|
2 | 2 |
|
3 |
| -/* Program to reverse a Stack using Recursion*/ |
4 | 3 | import java.util.Stack;
|
5 | 4 |
|
| 5 | +/** |
| 6 | + * Class that provides methods to reverse a stack using recursion. |
| 7 | + */ |
6 | 8 | public final class ReverseStackUsingRecursion {
|
7 | 9 | private ReverseStackUsingRecursion() {
|
8 | 10 | }
|
9 | 11 |
|
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"); |
24 | 21 | }
|
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); |
33 | 26 | }
|
34 | 27 | }
|
35 | 28 |
|
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) { |
51 | 36 | if (stack.isEmpty()) {
|
52 |
| - stack.push(temptop); // If stack is empty push the element |
| 37 | + stack.push(element); |
53 | 38 | } 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); |
61 | 42 | }
|
62 | 43 | }
|
63 | 44 | }
|
0 commit comments