个人算法小题小结


//两个栈实现队列功能
public class TestALL {
public Stack<Integer> firstStack;
public Stack<Integer> secondStack;
public TestALL(){
firstStack=new Stack<Integer>();
secondStack=new Stack<Integer>();

}
public void add(int scannerInt){
firstStack.push(scannerInt);
}
public int poll(){
if(firstStack.isEmpty()&&secondStack.isEmpty()){
throw new RuntimeException("栈为空");
}else if(secondStack.isEmpty()){
while(!firstStack.isEmpty()){
secondStack.push(firstStack.pop());
}
}
return secondStack.pop();
}
public int peek(){
if(firstStack.isEmpty()&&secondStack.isEmpty()){
throw new RuntimeException("栈为空");
}else if(secondStack.isEmpty()){
while(!firstStack.isEmpty()){
secondStack.push(firstStack.pop());
}
}
return secondStack.peek();
}
}
posted @ 2017-06-19 18:39  不会就问咯  阅读(192)  评论(0编辑  收藏  举报