如何仅用递归函数和栈操作逆序一个栈

3.如何仅用递归函数和栈操作逆序一个栈

题目:

一个栈一次压入了1、2、3、4、5,那么从栈顶到栈底分别为5、4、3、2、1.将这个栈转置后,从栈顶到栈底为1、2、3、4、5,也就是实现栈中元素的逆序,但是只能用递归函数来实现,不能用其他数据结构。

解题:

/**
 * 一个栈一次压入了1、2、3、4、5,那么从栈顶到栈底分别为5、4、3、2、1.将这个栈转置后,
 * 从栈顶到栈底为1、2、3、4、5,
 * 也就是实现栈中元素的逆序,但是只能用递归函数来实现,不能用其他数据结构。
 * @author dream
 *
 */
public class Problem03_ReverseStackUsingRecursive {

    public static void reverse(Stack<Integer> stack) {
        if (stack.isEmpty()) {
            return;
        }
        int i = getAndRemoveLastElement(stack);
        reverse(stack);     
        stack.push(i);
    }

    /**
     * 这个函数就是删除栈底元素并返回这个元素
     * @param stack
     * @return
     */
    public static int getAndRemoveLastElement(Stack<Integer> stack) {
        int result = stack.pop();
        if (stack.isEmpty()) {
            return result;
        } else {
            int last = getAndRemoveLastElement(stack);
            stack.push(result);
            return last;
        }
    }

    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());
        }

    }
    
}

    原文作者:仇诺伊
    原文地址: https://www.jianshu.com/p/b44912299aaa
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞