# LeetCode-Implement Queue using Stacks-解题报告

Implement the following operations of a queue using stacks.

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

class Queue {
public:
// Push element x to the back of queue.
void push(int x) {
one.push(x);
}

// Removes the element from in front of queue.
void pop(void) {
if (!two.empty())
{
two.pop();
return;
}
while (!one.empty())
{
two.push(one.top());
one.pop();
}
if (!two.empty())two.pop();
}

// Get the front element.
int peek(void) {
if (!two.empty())return two.top();
while (!one.empty())
{
two.push(one.top());
one.pop();
}
if (!two.empty())return two.top();
}

// Return whether the queue is empty.
bool empty(void) {
return one.empty() && two.empty();
}
private:
stack<int>one;
stack<int>two;
};

#### LeetCode-Implement Queue using Stacks

2015-08-13 20:08:16

#### leetcode-Implement Queue using Stacks

2015-11-04 22:24:28

#### LEETCODE-Implement Queue using Stacks

2015-10-10 15:36:47

#### Leetcode-Implement Queue using Stacks（C++）

2015-07-18 20:24:38

#### LeetCode-Implement Stack using Queues-解题报告

2015-06-29 21:58:15

#### COCI2013/2014#ROUND1完整解题报告

2017-02-21 20:59:54

#### Leetcode_232_Implement Queue using Stacks

2015-09-12 11:01:23

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

2016-06-04 23:32:59

#### LeetCode 232： Implement Queue using Stacks

2015-07-07 19:35:06

#### 【LeetCode-232】 Implement Queue using Stacks(C++)

2016-09-03 17:37:16