如何仅用队列结构实现栈结构?
如何仅用栈结构实现队列结构?
public static class TwoQueuesStack {
private Queue<Integer> data;
private Queue<Integer> help;
public TwoQueuesStack() {
data = new LinkedList<Integer>();
help = new LinkedList<Integer>();
}
public void push(int pushInt) {
data.add(pushInt);
}
public int peek() {
if (data.isEmpty()) {
throw new RuntimeException("Stack is empty!");
}
while (data.size() != 1) {
help.add(data.poll());
}
int res = data.poll();
help.add(res);
swap();
return res;
}
public int pop() {
if (data.isEmpty()) {
throw new RuntimeException("Stack is empty!");
}
while (data.size() > 1) {
help.add(data.poll());
}
int res = data.poll();
swap();
return res;
}
private void swap() {//换下引用
Queue<Integer> tmp = help;
help = data;
data = tmp;
}
}
public static class TwoStacksQueue {
private Stack<Integer> stackPush;
private Stack<Integer> stackPop;
public TwoStacksQueue() {
stackPush = new Stack<Integer>();
stackPop = new Stack<Integer>();
}
public void push(int pushInt) {
stackPush.push(pushInt);
}
public int poll() {
if (stackPop.empty() && stackPush.empty()) {
throw new RuntimeException("Queue is empty!");
// } else if (stackPop.empty()) {
// while (!stackPush.empty()) {
// stackPop.push(stackPush.pop());
// }
// }
dao();
return stackPop.pop();
}
public int peek() {
if (stackPop.empty() && stackPush.empty()) {
throw new RuntimeException("Queue is empty!");
} else if (stackPop.empty()) {
while (!stackPush.empty()) {
stackPop.push(stackPush.pop());
}
}
return stackPop.peek();
}
public void dao(){
if(!stackPop.isEmpty()){
return;
}
while(!stackPush.isEmpty()){
stackPop.push(stackPush.pop());
}
}
}