比较简单,就不多废话了,直接贴源码!
package com.robin.stack;
import java.util.Stack;
public class MyQueue {
private Stack s1 = new Stack();
private Stack s2 = new Stack();
public synchronized void put(E e){
s1.push(e);
}
public synchronized E pop(){
if(s2.empty()){
while(!s1.isEmpty()){
s2.push(s1.pop());
}
}
return s2.pop();
}
public synchronized boolean empty(){
return s1.isEmpty() && s2.isEmpty();
}
public static void main(String[] args) {
MyQueue mq = new MyQueue();
mq.put(1);
mq.put(2);
mq.put(3);
int data1 = mq.pop();
int data2 = mq.pop();
int data3 = mq.pop();
System.out.println(data1 + "\t" +data2+"\t"+data3);
}
}