package stackgame;
import java.util.Stack;
public class twostackQueue {
private Stack<Integer> stackPush;
private Stack<Integer> stackPop;
public twostackQueue() {
// TODO Auto-generated constructor stub
this.stackPush=new Stack<Integer>();
this.stackPop=new Stack<Integer>();
}
public void add(int i) {
stackPush.push(i);
}
public int poll() {
if(stackPush.empty()&&stackPop.empty()) {
throw new RuntimeException("Queue is empty.");
}
if(stackPop.empty())
stackPop.push(stackPush.pop());
return stackPop.pop();
}
public int peek() {
if(stackPush.empty()&&stackPop.empty()) {
throw new RuntimeException("Queue is empty.");
}
if(stackPop.empty())
stackPop.push(stackPush.pop());
return stackPop.peek();
}
public static void main(String[] args) {
twostackQueue que=new twostackQueue();
que.add(1);
que.add(2);
que.add(3);
que.add(4);
que.add(5);
System.out.println(que.peek());
System.out.println(que.poll());
System.out.println(que.peek());
}
}
运行结果
5 5 4