java实现——007用两个栈实现队列

 import java.util.Stack;

 public class T007 {
public static void main(String[] args) {
Queue q = new Queue();
q.stack1 = new Stack();
q.stack2 = new Stack();
q.appendTail(1);
System.out.println(q.stack1.toString());
System.out.println(q.stack2.toString());
q.appendTail(2);
q.appendTail(3);
q.deleteHead();
System.out.println(q.stack1.toString());
System.out.println(q.stack2.toString());
} public static class Queue {
private Stack<Integer> stack1;
private Stack<Integer> stack2; public void appendTail(int e) {
stack1.push(e);
} public void deleteHead() {
if (stack2.size() <= 0) {
while (stack1.size() > 0) {
int data = stack1.peek();
stack1.pop();
stack2.push(data);
}
}
if (stack2.size() == 0)
System.out.println("queue is empty");
int head = stack2.peek();
stack2.pop();
}
}
}
上一篇:【poj1186】 方程的解数


下一篇:jvm系列 (四) ---强、软、弱、虚引用