# 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

## leetcode 225: Implement Stack using Queues

leetcode 225: Implement Stack using Queues python java c++
• xudli
• 2015年06月19日 01:00
• 4429

## LeetCode 225: Implement Stack using Queues

Implement the following operations of a stack using queues. push(x) -- Push element x onto stack....
• sunao2002002
• 2015年06月13日 14:58
• 5315

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

【题目】 Implement the following operations of a stack using queues. push(x) -- Push element x o...
• ljiabin
• 2015年06月14日 10:35
• 8816

## <LeetCode OJ> 225. Implement Stack using Queues

225. Implement Stack using Queues My Submissions Question Total Accepted: 26676 Total Submission...
• EbowTang
• 2016年01月01日 14:52
• 747

## Leetcode_252_Implement Stack using Queues

• pistolove
• 2015年09月20日 18:21
• 963

## 225. Implement Stack using Queues

Implement the following operations of a stack using queues. push(x) – Push element x onto stack. pop...
• u010429424
• 2017年06月01日 10:57
• 129

## Implement Stack using Queues

Implement the following operations of a stack using queues.push(x) – Push element x onto stack. pop...
• zuoyexingchennn
• 2015年06月16日 10:48
• 180

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

Implement the following operations of a stack using queues. push(x) -- Push element x onto stack....
• xy010902100449
• 2015年10月21日 18:07
• 528

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

• coder_orz
• 2016年06月04日 23:32
• 1076

## leetcode--Implement Stack using Queues

mplement the following operations of a stack using queues. push(x) -- Push element x onto stack.pop...
• hello_worldee
• 2017年08月08日 14:30
• 77

举报原因： 您举报文章：LeetCode225:Implement Stack using Queues 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)