不是用辅助的数据结构逆序一个栈

package com.hzins.suanfa;

import java.util.Stack;
/**
 * 逆序一个栈
 * @author Administrator
 *
 */
public class DiguiStack {
    /**
     * 将栈中的元素从底抽出
     * @param stack
     * @return
     */
    public static int getLastEntry(Stack<Integer> stack){
        int result = stack.pop();
        //递归函数栈底
        if(stack.isEmpty()){
            return result;
        }
        //层级之间
        else{
            //函数栈底会返回底层数字last  栈底上一层保存pop出的数 result
            int last = getLastEntry(stack);
            stack.push(result);
            return last;
        }
    }
    /**
     * 逆序一个栈
     * @param stack
     */
    public static void getStackreverse(Stack<Integer> stack){
        if(stack.isEmpty()){
            return ;
        }
        int entry = getLastEntry(stack);
        getStackreverse(stack);
        stack.push(entry);
    }
    public static void main(String[] args) {
        Stack<Integer> stack = new  Stack<Integer>();
        stack.push(1);
        stack.push(2);
        stack.push(3);
        System.out.println(getLastEntry(stack));
        System.out.println(stack.pop() + "   " + stack.pop());
    }
}
原文地址:https://www.cnblogs.com/caobojia/p/6764139.html