//两个栈实现队列功能
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();
}
}

相关文章:

  • 2021-12-23
  • 2022-01-01
  • 2021-07-27
  • 2021-04-02
  • 2021-08-23
  • 2021-10-23
  • 2021-10-23
  • 2022-02-22
猜你喜欢
  • 2022-01-04
  • 2022-12-23
  • 2021-11-09
  • 2021-05-22
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案