###### leetcode 日经贴，Cpp code -Implement Queue using Stacks

class Queue {
stack<int> st[2];
int inidx, outidx;
void move(int from, int to) {
while (!st[from].empty()) {
st[to].push(st[from].top());
st[from].pop();
}
}
public:
Queue() {
inidx = 0;
outidx = 1;
}
// Push element x to the back of queue.
void push(int x) {
st[inidx].push(x);
}

// Removes the element from in front of queue.
void pop(void) {
if (st[outidx].empty() && !st[inidx].empty()) {
move(inidx, outidx);
}
if (!st[outidx].empty()) {
st[outidx].pop();
}
}

// Get the front element.
int peek(void) {
if (st[outidx].empty() && !st[inidx].empty()) {
move(inidx, outidx);
}
if (!st[outidx].empty()) {
return st[outidx].top();
} else {
return -1;
}
}

// Return whether the queue is empty.
bool empty(void) {
return st[inidx].empty() && st[outidx].empty();
}
};

#### LeetCode 232： Implement Queue using Stacks

2015-07-07 19:35:06

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

2016-06-04 23:32:59

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

2016-09-03 17:37:16

#### Leetcode_232_Implement Queue using Stacks

2015-09-12 11:01:23

#### leetcode 232:Implement Queue using Stacks

2015-07-07 04:48:38

#### LeetCode232 Implement Queue using Stacks Java 题解

2015-07-09 12:03:51

#### LeetCode232——Implement Queue using Stacks

2015-07-31 16:48:03

#### LeetCode 225: Implement Stack using Queues

2015-06-13 14:58:28

#### implement a queue by using two stacks

2012-08-29 10:02:32

#### Chapter3--Stacks and Queues

2013-12-17 15:08:05

## 不良信息举报

leetcode 日经贴，Cpp code -Implement Queue using Stacks