import java.util.Stack;
/**
* 用栈实现队列
*/
public class StackToQueue {
public static class MyQueue<T> {
public Stack<T> data;
public Stack<T> help;
public int size;
public MyQueue() {
data = new Stack<>();
help = new Stack<>();
}
public void push(T value) {
if (help.isEmpty()) {
data.push(value);
} else {
help.push(value);
}
size++;
}
public T poll() {
if (isEmpty()) {
System.out.println("the queue is empty");
return null;
}
size--;
for (int i = 0; i < size; i++) {
help.push(data.pop());
}
T value = data.pop();
for (int i = 0; i < size; i++) {
data.push(help.pop());
}
return value;
}
public boolean isEmpty() {
return size == 0;
}
}
}
/* 如有错误,欢迎批评指正 */