比较简单,就不多废话了,直接贴源码!
package com.robin.stack;
import java.util.Stack;
public class MyQueue<E> {
private Stack<E> s1 = new Stack<E>();
private Stack<E> s2 = new Stack<E>();
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<Integer> mq = new MyQueue<Integer>();
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);
}
}