Header Ads Widget

Responsive Advertisement

Custom Collection class reverse a stack

how to reverse a stack using recursion, both for strings and integers. Here’s an explanation of how the stack reversal works for both cases.

Optimizations or Improvements:

  1. Avoid Popping in Recursion:

ü  Instead of popping in the recursive method and then printing after, you could build a new stack as part of the reversal process without printing during recursion.

  1. Generalized Method:

ü  You can create a single method that works with any data type using generics, instead of having separate methods for String and Integer. Here's how to do that:

 

java

public class StackReversalGeneric<T> {

 

    public void stackReversal(Stack<T> s) {

        if (s.isEmpty()) return;

        T n = getLast(s);

        stackReversal(s);

        s.push(n);

    }

 

    public T getLast(Stack<T> s) {

        T a = s.pop();

        if (s.isEmpty()) {

            return a;

        } else {

            T k = getLast(s);

            s.push(a);

            return k;

        }

    }

 

    public static void main(String[] args) {

        Stack<String> stack = new Stack<>();

        stack.push("Kartik");

        stack.push("Chandra");

        stack.push("Mandal");

        stack.push("Java");

        stack.push("J2EE");

        stack.push("Soap");

        stack.push("Rest");

 

        StackReversalGeneric<String> reversal = new StackReversalGeneric<>();

        reversal.stackReversal(stack);

 

        while (!stack.isEmpty()) {

            System.out.println(stack.pop());

        }

    }

}

 

This generic method can work for any type (String, Integer, etc.) and avoids code duplication.


Explanation of StackReversalString

  1. Recursive Reversal Approach:

ü  The function stackReversal is designed to reverse a stack by recursively removing the last element, calling itself on the remaining stack, and then pushing the element back after the recursion completes.

  1. Method getLast:

ü  This helper method retrieves the last element of the stack, using recursion, without modifying the relative order of other elements in the stack.

ü  It pops elements until the stack is empty, then rebuilds the stack as the recursion unwinds.

  1. Final Stack Printing:

ü  After the recursion completes, the elements of the stack are printed in reverse order by continuously popping elements from the stack.


Code Walkthrough:

For the string-based reversal:

java

Stack <String> stack = new Stack <String> ();

stack.push("Kartik");

stack.push("Chandra");

stack.push("Mandal");

stack.push("Java");

stack.push("J2EE");

stack.push("Soap");

stack.push("Rest");

 

The stack is initially:

Rest

 

Soap

 

J2EE

 

Java

 

Mandal

 

Chandra

 

Kartik

 

After applying the recursive reversal logic:

  1. The last element ("Rest") is extracted using getLast.
  2. stackReversal is called recursively on the remaining elements.
  3. After recursion, the last element is pushed back onto the stack.

When printing the stack, we pop each element, so the output becomes:

Plain text

Rest

Soap

J2EE

Java

Mandal

Chandra

Kartik

 


Explanation of StackReversalInteger

The integer version of the stack reversal works in exactly the same way as the string version, except it operates on integers.

Example:

java

Stack <Integer> stack = new Stack <Integer> ();

stack.push(1);

stack.push(2);

stack.push(3);

stack.push(4);

stack.push(9);

stack.push(5);

stack.push(8);

 

The initial stack is:

8

 

5

 

9

 

4

 

3

 

2

 

1

 

The reversal logic will give the output:

8

5

9

4

3

2

1

 


Notes:

Ø  Recursion Depth: Both implementations use recursion, which could lead to a StackOverflowError if the stack is too deep (i.e., it has too many elements). For large stacks, an iterative solution might be preferable.

Ø  Final Popping: In both examples, after the recursive reversal, the stack is emptied and printed.


reverse a stack
reverse a stack



 

Post a Comment

0 Comments