/**
* 将倒序并且移除最后一个元素
* @author yyq
*
*/
public class StackOrderDemo {
public Integer getAndRemoveLastElement(Stack<Integer> stack){
if(stack.isEmpty()){
throw new RuntimeException("stack is empty");
}
int result = stack.pop();
if(stack.isEmpty()){
return result;
}else{
int last = getAndRemoveLastElement(stack);
stack.push(result);
return last;
}
}
}
用递归函数将栈倒序
最新推荐文章于 2020-08-27 09:12:39 发布