package com.mfq.stack;
import java.util.Stack;
/**
* 用两个栈实现队列
*/
public class TwoStackImplQueue {
private Stack<Integer> stackPush;
private Stack<Integer> stackPop;
public TwoStackImplQueue() {
this.stackPush = new Stack<>();
this.stackPop = new Stack<>();
}
public void add(int data) {
stackPush.push(data);
}
public int poll() {
if (stackPush.isEmpty() && stackPop.isEmpty()) {
throw new RuntimeException("queue is empty");
} else if (stackPop.isEmpty()) {
while (!stackPush.isEmpty()) {
stackPop.push(stackPush.pop());
}
}
return stackPop.pop();
}
public int peek() {
if (stackPush.isEmpty() && stackPop.isEmpty()) {
throw new RuntimeException("queue is empty");
} else if (stackPop.isEmpty()) {
while (!stackPush.isEmpty()) {
stackPop.push(stackPush.pop());
}
}
return stackPop.pop();
}
}
用两个栈实现队列
最新推荐文章于 2024-02-21 17:10:17 发布