|
1 | 1 | package com.thealgorithms.datastructures.stacks;
|
2 | 2 |
|
3 |
| -import java.util.Scanner; |
4 | 3 | import java.util.Stack;
|
5 | 4 |
|
6 | 5 | /**
|
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. |
8 | 23 | *
|
9 | 24 | * @author Ishika Agarwal, 2021
|
10 | 25 | */
|
11 | 26 | public final class ReverseStack {
|
12 | 27 | private ReverseStack() {
|
13 | 28 | }
|
14 | 29 |
|
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) { |
35 | 41 | if (stack.isEmpty()) {
|
36 | 42 | return;
|
37 | 43 | }
|
38 | 44 |
|
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(); |
45 | 46 | reverseStack(stack);
|
46 |
| - |
47 |
| - // Insert the topmost element to the bottom of the stack |
48 | 47 | insertAtBottom(stack, element);
|
49 | 48 | }
|
50 | 49 |
|
| 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 | + */ |
51 | 64 | private static void insertAtBottom(Stack<Integer> stack, int element) {
|
52 | 65 | if (stack.isEmpty()) {
|
53 |
| - // When stack is empty, insert the element so it will be present at |
54 |
| - // the bottom of the stack |
55 | 66 | stack.push(element);
|
56 | 67 | return;
|
57 | 68 | }
|
58 | 69 |
|
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(); |
63 | 71 | insertAtBottom(stack, element);
|
64 |
| - |
65 |
| - stack.push(ele); |
| 72 | + stack.push(topElement); |
66 | 73 | }
|
67 | 74 | }
|
0 commit comments