# LeetCode225:Implement Stack using Queues

Implement the following operations of a stack using queues.

• push(x) – Push element x onto stack.
• pop() – Removes the element on top of the stack.
• top() – Get the top element.
• empty() – Return whether the stack is empty.

Notes:

• You must use only standard operations of a queue – which means only
push to back, peek/pop from front, size, and is empty operations are
valid.
• Depending on your language, queue may not be supported natively. You
may simulate a queue by using a list or deque (double-ended queue),
as long as you use only standard operations of a queue. You may
assume that all operations are valid (for example, no pop or top
operations will be called on an empty stack).

Update (2015-06-11):
The class name of the Java function had been updated to MyStack instead of Stack.

Credits:
Special thanks to @jianchao.li.fighter for adding this problem and all test cases.

Hide Tags Stack Data Structure

C++代码如下：
runtime:0ms

class Stack {
public:
// Push element x onto stack.
void push(int x) {
if(queueOne.empty())
{
queueTwo.push(x);
}else
{
queueOne.push(x);
}
}

// Removes the element on top of the stack.
void pop() {
if(queueOne.empty())
{
while(queueTwo.size()!=1)
{
queueOne.push(queueTwo.front());
queueTwo.pop();
}
queueTwo.pop();
}
else
{
while(queueOne.size()!=1)
{
queueTwo.push(queueOne.front());
queueOne.pop();
}
queueOne.pop();
}
}

// Get the top element.
int top() {
if(queueOne.empty())
{
while(queueTwo.size()!=1)
{
queueOne.push(queueTwo.front());
queueTwo.pop();
}
int result=queueTwo.front();
queueOne.push(queueTwo.front());
queueTwo.pop();
return result;
}
else
{
while(queueOne.size()!=1)
{
queueTwo.push(queueOne.front());
queueOne.pop();
}
int result=queueOne.front();
queueTwo.push(queueOne.front());
queueOne.pop();
return result;
}
}

// Return whether the stack is empty.
bool empty() {
return queueOne.empty()&&queueTwo.empty();
}

private:
queue<int> queueOne;
queue<int> queueTwo;
};

C++代码如下：
runtime:0ms

class Stack {
public:
// Push element x onto stack.
void push(int x) {
q.push(x);
for(int i=0;i<q.size()-1;i++)
{
q.push(q.front());
q.pop();
}
}

// Removes the element on top of the stack.
void pop() {
q.pop();
}

// Get the top element.
int top() {
return q.front();
}

// Return whether the stack is empty.
bool empty() {
return q.empty();
}

private:
queue<int> q;
};

#### LeetCode 225: Implement Stack using Queues

2015-06-13 14:58:28

#### [LeetCode-225] Implement Stack using Queues（两个队列实现栈）

2015-10-21 18:07:39

#### leetcode 225: Implement Stack using Queues

2015-06-19 01:00:54

#### 【LeetCode】Implement Stack using Queues 解题报告

2015-06-14 10:35:53

#### 225. Implement Stack using Queues

2016-03-16 13:33:30

#### 232. Implement Queue using Stacks [easy] (Python)

2016-06-04 23:32:59

#### LeetCode-Implement_Stack_using_Queues

2018-04-11 09:43:23

#### 225. Implement Stack using Queues [easy] (Python)

2016-06-07 16:57:33

#### Leetcode 225:Implement Stack using Queues

2016-02-25 14:39:32

#### 225. Implement Stack using Queues 用两个队列实现栈的基本操作。

2016-02-18 23:25:45