# LeetCode 225.用队列实现栈
题目:
```
使用队列实现栈的下列操作:
push(x) -- 元素 x 入栈
pop() -- 移除栈顶元素
top() -- 获取栈顶元素
empty() -- 返回栈是否为空
注意:
你只能使用队列的基本操作-- 也就是 push to back, peek/pop from front, size, 和 is empty 这些操作是合法的。
你所使用的语言也许不支持队列。 你可以使用 list 或者 deque(双端队列)来模拟一个队列 , 只要是标准的队列操作即可。
你可以假设所有操作都是有效的(例如, 对一个空的栈不会调用 pop 或者 top 操作)。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/implement-stack-using-queues
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
```
## 双队列解法(1)
队列是FIFO的,栈是LIFO的,要用队列来实现栈,和题目232一样,这道题首先想到是双队列,A队列用作入栈,B队列用作出栈,但是队列和栈又不同,队列是一直都是先进先出,你即使重新将队列A入栈到队列B,那么他还是先进先出,所以这里入栈还是向队列A入栈,只有出栈的时候需要做些改动,将队列A只留下最后一个元素用作最后出队,其他元素入栈到B队列中,这样出栈的就是最后一个元素了.
```java
class MyStack {
Queue<Integer> inQueue = new LinkedList<>();
Queue<Integer> outQueue = new LinkedList<>();
Integer top = -1;
/** Initialize your data structure here. */
public MyStack() {
}
/** Push element x onto stack. */
public void push(int x) {
inQueue.offer(x);
top = x;
}
/** Removes the element on top of the stack and returns that element. */
public int pop() {
//保留最后一个,用作最后出栈,即达到先入后出的效果
while (inQueue.size() > 1){
int temp = inQueue.poll();
outQueue.offer(temp);
top = temp;
}
Integer result = inQueue.poll();
//因为outQueue和原来的inqueue是一样的,所以这里直接将outQueue赋值给inQueue,这样避免了再次复制元素
Queue<Integer> temp = inQueue;
inQueue = outQueue;
outQueue = temp;
return result;
}
/** Get the top element. */
public int top() {
return top;
}
/** Returns whether the stack is empty. */
public boolean empty() {
return inQueue.isEmpty();
}
}
```
### 复杂度
时间复杂度:入栈O(1),出栈O(N)
空间复杂度:O(N)
## 双队列解法(2)
这里需要改变的是入栈的时候就达到栈的效果,每次入队的时候先入到B队列中,这样他就是第一个元素,然后将剩下的元素再一一入队到B队列,达到后进排队在最前面的效果
```java
class MyStack2 {
Queue<Integer> inQueue = new LinkedList<>();
Queue<Integer> outQueue = new LinkedList<>();
/** Initialize your data structure here. */
public MyStack2() {
}
/** Push element x onto stack. */
public void push(int x) {
outQueue.offer(x);
while(!inQueue.isEmpty()){
outQueue.offer(inQueue.poll());
}
Queue<Integer> temp = outQueue;
outQueue = inQueue;
inQueue = temp;
}
/** Removes the element on top of the stack and returns that element. */
public int pop() {
return inQueue.poll();
}
/** Get the top element. */
public int top() {
return inQueue.peek();
}
/** Returns whether the stack is empty. */
public boolean empty() {
return inQueue.isEmpty();
}
}
```
### 复杂度
空间复杂度:O(N)
时间复杂度:入栈O(N),出栈O(1)
## 单队列解法
每次有元素需要入栈的时候,先将元素入队,然后循环队列并到最后一个元素,保留最后一个元素,之前的元素全部都入队到队列该元素之后,就是每次有新元素入队的时候,前面的元素全部出队,并重新入队,这样保证最后入队的元素排在最前
```java
class MyStack3 {
Queue<Integer> inQueue = new LinkedList<>();
/** Initialize your data structure here. */
public MyStack3() {
}
/** Push element x onto stack. */
public void push(int x) {
inQueue.offer(x);
int size = inQueue.size();
//每个数值出队之后再重新入队,达到最后面进来的元素在最前面的效果
while(size > 1){
inQueue.offer(inQueue.poll());
size--;
}
}
/** Removes the element on top of the stack and returns that element. */
public int pop() {
return inQueue.poll();
}
/** Get the top element. */
public int top() {
return inQueue.peek();
}
/** Returns whether the stack is empty. */
public boolean empty() {
return inQueue.isEmpty();
}
}
```
### 复杂度
时间复杂度:入栈O(N),出栈O(1)
空间复杂度:O(N)
LeetCode 225.用队列实现栈