由两个栈实现队列
import java.util.Stack;public class sAD02 { public Stack<Integer> stackPush; public Stack<Integer> stackPop; public sAD02(){ stackPush = new Stack<Integer>(); stackPop = new Stack<Integer>(); } public void add(int pushInt){ stackPush.push(pushInt); } public int poll(){ if(stackPop.empty() && stackPush.empty()){ throw new RuntimeException("Queue is empty"); }else if(stackPop.empty()){ while (!stackPush.empty()){ stackPop.push(stackPush.pop()); } } return stackPop.pop(); } public int peek(){ if(stackPop.empty() && stackPush.empty()){ throw new RuntimeException("Queue is empty"); }else if(stackPop.empty()){ while(! stackPush.empty()){ stackPop.push(stackPush.pop()); } } return stackPop.peek(); }}新闻热点
疑难解答