第一章 栈和队列
1.2 由两个栈组成的队列
【题目】
编写一个类,用两个栈实现队列,支持队列的基本操作(add、poll、peek)
【难度】
尉 ★★☆☆
【题解】
队列的特点是先进先出,而栈的特点是先进后出。借助两个栈把顺序颠倒过来即可实现队列的操作。其中一个栈作为压入栈,在压入数据时只往这个栈中压入,记为 stackPush;另一个栈只作为弹出栈,在弹出数据时只从这个栈弹出,记为stackPop。在栈中,顺序是先进后出的,只要把 stackPush 的数据依次弹出再压入 stackPop,对于整体而言,顺序就变为先进先出了,就像队列一样。但是必须满足以下两点:
- 如果 stackPush 要往 stackPop 中压入数据,那么必须一次性把 stackPush 中的数据全部压入 stackPop 中;
- 如果 stackPop 不为空,那么 stackPush 绝对不能向 stackPop 中压入数据。
如果违反了以上两点任意一点都会发生错误。
【实现】
- MyQueue.java
public interface MyQueue {
/**
* 入队
*
* @param pushNum 新数据插入队尾
* @throws Exception
*/
void push(int pushNum) throws Exception;
/**
* 出队
*
* @return 队首元素出队
* @throws Exception
*/
int pop() throws Exception;
/**
* 获取队首元素
*
* @return 返回队首元素
* @throws Exception
*/
int peek() throws Exception;
}
- AbstractMyQueue.java
import java.util.Stack;
public abstract class AbstractMyQueue implements MyQueue {
protected Stack<Integer> stackPush;
protected Stack<Integer> stackPop;
protected AbstractMyQueue() {
stackPush = new Stack<>();
stackPop = new Stack<>();
}
}
- MyQueue1.java
public class MyQueue1 extends AbstractMyQueue {
public MyQueue1() {}
@Override
public void push(int pushNum) throws Exception {
this.stackPush.push(pushNum);
}
@Override
public int pop() throws Exception {
if (this.stackPush.empty() && this.stackPop.empty()) {
throw new RuntimeException("队列为空");
}
while (!this.stackPush.empty()) {
this.stackPop.push(this.stackPush.pop());
}
return this.stackPop.pop();
}
@Override
public int peek() throws Exception {
if (this.stackPush.empty() && this.stackPop.empty()) {
throw new RuntimeException("队列为空");
}
while (!this.stackPush.empty()) {
this.stackPop.push(this.stackPush.pop());
}
return this.stackPop.peek();
}
}
- MyQueueTest.java
public class MyQueueTest {
public static void main(String[] args) throws Exception {
MyQueue queue = new MyQueue1();
test(queue1);
}
private static void test(MyQueue queue) throws Exception {
if (queue == null) {
throw new RuntimeException("参数不合法!");
}
int popNum = -1;
queue.push(1);
queue.push(2);
queue.push(3);
queue.push(4);
popNum = queue.pop();
queue.push(5);
popNum = queue.pop();
queue.push(6);
popNum = queue.pop();
queue.push(7);
popNum = queue.pop();
queue.push(8);
popNum = queue.pop();
queue.push(9);
}
}