个人算法小题小结


//两个栈实现队列功能
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();
}
}
原文地址:https://www.cnblogs.com/KingIceMou/p/7050021.html