This action will force synchronization from wangjili/Algorithm, which will overwrite any changes that you have made since you forked the repository, and can not be recovered!!!
Synchronous operation will process in the background and will refresh the page when finishing processing. Please be patient.
package nowCoderClass1.section4;import java.util.Stack;/*** 实现一个栈的逆序,但是只能用递归函数和这个栈本身的pop操作来实现,而不能自己申请另外的数据结构。* Created by Dell on 2017年05月07日.*/public class StackReverse {public int[] reverseStack(int[] A, int n) {// write code hereStack<Integer> stack=new Stack<Integer>();//将数组放入栈中for(int i=0;i<n;i++){stack.push(A[i]);}System.out.print("逆序前"+stack.peek());//将栈逆序reverse(stack);System.out.print("逆序后"+stack.peek());//将栈中的数放入数组,返回for(int i=n-1;i>=0;i--){A[i]=stack.pop();}return A;}/*** 使用get方法每次获取最下面的元素,然后将栈里的元素逆序*/private void reverse(Stack<Integer> stack) {int result=get(stack);if(stack.isEmpty())stack.push(result);else{reverse(stack);stack.push(result);}}/*** 将栈反转,使用下列的办法无法将栈反转,只是按原来的顺序压入栈*/// private void reverse(Stack<Integer> stack){// int result=(Integer)stack.pop();// if(stack.isEmpty()){// stack.push(result);// }else{// reverse(stack);// stack.push(result);// }// }/*** get()方法,使用递归将栈底元素拿出,将其他元素依次压入栈中*/private int get(Stack stack){int result=(Integer)stack.pop();//弹出的是当前的栈顶if(stack.isEmpty()){//到了最底层,返回的就是要return result;}else{int last=get(stack);//一层一层,直到取到栈底,然后将栈底元素一层一层往上传,传的过程中顺便将每一层的元素压栈stack.push(result);return last;}}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。