18.5: Given a stack, please reverse the stack, you cannot apply for additional data structures, only recursive functions can be used

Given a stack, please reverse the stack, you cannot apply for additional data structures, only recursive functions can be used

package algorithmbasic.class18;

import java.util.Stack;

//给定一个栈,请逆序这个栈,不能申请额外的数据结构,只能使用递归函数
public class ReverseStackUsingRecursive {
    
    

    public static void reverse(Stack<Integer> stack) {
    
    
        if (stack.isEmpty()) {
    
    
            return;
        }
        //拿到栈底元素lowValue。
        int lowValue = f(stack);
        reverse(stack);
        stack.push(lowValue);
    }

    //黑盒:给我一个栈,在保证栈中元素顺序不变的情况下,弹出栈底元素。
    public static int f(Stack<Integer> stack) {
    
    
        int result = stack.pop();
        if (stack.isEmpty()) {
    
    
            return result;
        }
        int last = f(stack);
        stack.push(result);
        return last;
    }
    
    // -------------------- for test ------------------------
    
    public static void main(String[] args) {
    
    
        Stack<Integer> test = new Stack<Integer>();
        test.push(1);
        test.push(2);
        test.push(3);
        test.push(4);
        test.push(5);
        reverse(test);
        while (!test.isEmpty()) {
    
    
            System.out.println(test.pop());
        }

    }
}

reverse main function:

Suppose we have an f method: we can get the bottom element of the stack and ensure that the order of other elements in the stack remains unchanged.

Call the f method when you come up, and get the lowValue element at the bottom of the stack. At this time, the bottom element in the stack is taken away.

This recursion keeps calling the f method until the stack is empty. That is: in the process of passing, all the elements at the bottom of the stack are collected, and the order is exactly reversed from front to back.

Then when returning, just push the bottom element of the stack directly into the stack.

insert image description here

f function:

The function of the f function is: you can get the bottom element of the stack, and ensure that the order of other elements in the stack remains unchanged.

The first thing to enter is the first layer, and two variables are defined, one is result: records the elements of the current layer, and the other is last: records the elements at the bottom of the stack.

Pop up the elements of the first layer and put them in the result, and the result is the element of the current layer. At this time, the top of the stack has flown one, last = f(stack), continue to call recursion

Come to the second floor and continue the same work.

Keep recursing until the stack is empty. At this time, the bottom element of the stack is recorded in the result. Return the result at this time. Come to the second layer, at this time last receives the returned bottom element of the stack and returns last. Because the stack is empty at this time, the result is pushed onto the stack. Then return unanimously.

insert image description here

Guess you like

Origin blog.csdn.net/SCR1209/article/details/131112370