队列实现栈 | 栈实现队列
⼀、 ⽤栈实现队列
首先,队列的 API 如下:
class MyQueue {
/** 添加元素到队尾 */
public void push(int x);
/** 删除队头的元素并返回 */
public int pop();
/** 返回队头元素 */
public int peek();
/** 判断队列是否为空 */
public boolean empty();
}
我们使⽤两个栈 s1, s2 就能实现⼀个队列的功能(这样放置栈可能更容易理解) :
class MyQueue {
private Stack<Integer> s1, s2;
public MyQueue() {
s1 = new Stack<>();
s2 = new Stack<>();
}
// ...
}
当调用 push 让元素入队时,只要把元素压入 s1 即可,比如说 push 进 3 个元素分别是 1,2,3,那么底层结构就是这样:
/** 添加元素到队尾 */
public void push(int x) {
s1.push(x);
}
那么如果这时候使用 peek 查看队头的元素
怎么办呢?按道理队头元素应该是 1,但是在 s1 中 1 被压在栈底,现在就要轮到 s2 起到一个中转的作用了:当 s2
为空时,可以把 s1
的所有元素取出再添加进 s2
,这时候 s2
中元素就是先进先出顺序了。
/** 返回队头元素 */
public int peek() {
if (s2.isEmpty())
// 把 s1 元素压入 s2
while (!s1.isEmpty())
s2.push(s1.pop());
return s2.peek();
}
同理,对于 pop 操作,只要操作 s2 就可以了
/** 删除队头的元素并返回 */
public int pop() {
// 先调用 peek 保证 s2 非空
peek();
return s2.pop();
}
最后,如何判断队列是否为空呢?如果两个栈都为空的话,就说明队列为空:
/** 判断队列是否为空 */
public boolean empty() {
return s1.isEmpty() && s2.isEmpty();
}
C++代码:
class MyQueue {
public:
//主要思想是定义两个栈,一个用来存放入队元素的栈s1,另一个栈
//用来进行出队操作的s2,往队列中插元速时,直接往s1中插入,出队时,
//如果s2不为空,直接在s2中进行出栈操作,如果s2为空,就把s1中元素全部放到s2中
/** Initialize your data structure here. */
MyQueue() {
}
/** Push element x to the back of queue. */
void push(int x) {
//直接往s1中插入
s1.push(x);
}
/** Removes the element from in front of queue and returns that element. */
int pop() {
//进行出队操作时,如果s2为空,就直接把s1中元素先移动到s2中,
//在进行出队操作,如果s2不为空,直接在s2中直接出队,注意不能在s2有
//元素时把s1中的元素移动到s2中,这样顺序回乱
int data;
if(s2.empty())
{
while(s1.size())
{
s2.push(s1.top());
s1.pop();
}
data = s2.top();
s2.pop();
}
else
{
data = s2.top();
s2.pop();
}
return data;
}
/** Get the front element. */
int peek() {
int data;
if(s2.empty())
{
while(s1.size())
{
s2.push(s1.top());
s1.pop();
}
data = s2.top();
//s2.pop();
}
else
{
data = s2.top();
//s2.pop();
}
return data;
}
/** Returns whether the queue is empty. */
bool empty() {
return s1.empty() && s2.empty();
}
private:
stack<int> s1;
stack<int> s2;
};
二、用队列实现栈
首先看下栈的 API:
class MyStack {
/** 添加元素到栈顶 */
public void push(int x);
/** 删除栈顶的元素并返回 */
public int pop();
/** 返回栈顶元素 */
public int top();
/** 判断栈是否为空 */
public boolean empty();
}
先说 push
API,直接将元素加入队列,同时记录队尾元素
,因为队尾元素相当于栈顶元素,如果要 top 查看栈顶元素的话可以直接返回:
class MyStack {
Queue<Integer> q = new LinkedList<>();
int top_elem = 0;
/** 添加元素到栈顶 */
public void push(int x) {
// x 是队列的队尾,是栈的栈顶
q.offer(x);
top_elem = x;
}
/** 返回栈顶元素 */
public int top() {
return top_elem;
}
}
我们的底层数据结构是先进先出的队列,每次 pop 只能从队头取元素;但是栈是后进先出,也就是说 pop
API 要从队尾取元素
。
解决方法简单粗暴,把队列前面的都取出来再加入队尾,让之前的队尾元素排到队头
,这样就可以取出了:
/** 删除栈顶的元素并返回 */
public int pop() {
int size = q.size();
while (size > 1) {
q.offer(q.poll());
size--;
}
// 之前的队尾元素已经到了队头
return q.poll();
}
这样实现还有一点小问题就是,原来的队尾元素被提到队头并删除了,但是 top_elem 变量没有更新
,我们还需要一点小修改:
/** 删除栈顶的元素并返回 */
public int pop() {
int size = q.size();
// 留下队尾 2 个元素
while (size > 2) {
q.offer(q.poll());
size--;
}
// 记录新的队尾元素
top_elem = q.peek();
q.offer(q.poll());
// 删除之前的队尾元素
return q.poll();
}
最后,API empty 就很容易实现了,只要看底层的队列是否为空即可:
/** 判断栈是否为空 */
public boolean empty() {
return q.isEmpty();
}
当然可以用两个队列来实现一个栈:
C++代码:
class MyStack {
public:
//主要思想是定义两个队列,入栈时,如果两个队列同时为空,就随便往一个队列中插入
//如果其中有一个队列不为空,每次插就往非空的队列中插入。出栈时,每次把非空队列的
//前size-1个元素移动到空队列中,原非空队列就只剩下最后一个元素,这个元素满
//足栈的要求,直接出队。总之,主要思想是把两个队列中的元素来回移动,每次剩下最后
//一个元素直接出队,就相当于是栈顶元素
/** Initialize your data structure here. */
MyStack() {
queue<int> q1;
queue<int> q2;
}
/** Push element x onto stack. */
void push(int x) {
//如果两个队列都为空,随便插入到一个队列中,反之插入到非空队列中
if(!q1.empty())
{
q1.push(x);
}
else
{
q2.push(x);
}
}
/** Removes the element on top of the stack and returns that element. */
int pop() {
//每次把非空队列中的前size-1个元素移动到空队列中去,原非空队列
//剩下最后一个元素就相当于栈顶元素,出栈时,就直接出队
int data;
if(q1.empty())
{
while(q2.size() > 1)
{
q1.push(q2.front());
q2.pop();
}
data = q2.front();
q2.pop();
}
else
{
while(q1.size() > 1)
{
q2.push(q1.front());
q1.pop();
}
data = q1.front();
q1.pop();
}
return data;
}
/** Get the top element. */
int top() {
//返回非空队列的队尾元素,就相当于栈顶元素,
if(!q1.empty())
{
return q1.back();
}
else
{
return q2.back();
}
}
/** Returns whether the stack is empty. */
bool empty() {
//两个队列同时为空是,此时才表明栈中没有任何元素
return q1.empty() && q2.empty();
}
private:
queue<int> q1;
queue<int> q2;
};