Skip to content

Commit aa8451c

Browse files
committed
232 Implement Queue using Stacks
1 parent 5029c22 commit aa8451c

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@
128128
+ [228 Summary Ranges](algorithms/SummaryRanges)
129129
+ [230 Kth Smallest Element in a BST(BST、中序遍历)](algorithms/KthSmallestElementinaBST)
130130
+ [231 Power of Two(位运算,二进制1的个数)](algorithms/PowerofTwo)
131+
+ [232 Implement Queue using Stacks(栈模拟队列)](algorithms/ImplementQueueusingStacks)
131132

132133
## Database
133134

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
## Implement Queue using Stacks
2+
3+
Implement the following operations of a queue using stacks.
4+
5+
* push(x) -- Push element x to the back of queue.
6+
* pop() -- Removes the element from in front of queue.
7+
* peek() -- Get the front element.
8+
* empty() -- Return whether the queue is empty.
9+
Notes:
10+
11+
* 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.
12+
* 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.
13+
* You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).
14+
15+
## Solution
16+
17+
使用两个栈模拟队列
18+
19+
首先定义channel(first, second)操作,表示把第一个栈逐一弹出,并压入第二个栈:
20+
21+
```cpp
22+
void channel(stack<int> &src, stack<int> &dest) {
23+
while (!src.empty()) {
24+
dest.push(src.top());
25+
src.pop();
26+
}
27+
}
28+
29+
```
30+
31+
* push(), push时往第一个栈压入
32+
* empty(), 当且仅当两个栈都为空时,队列空
33+
* peek(), 若第二个栈为空,则channel(first, second), 返回second.top()
34+
* pop(), 若第二个栈为空,则channel(first, second), second.pop()
35+
36+
```cpp
37+
bool empty(void) {
38+
return first.empty() && second.empty();
39+
}
40+
void push(int x) {
41+
first.push(x);
42+
}
43+
int peek(void) {
44+
if (second.empty()) {
45+
channel(first, second);
46+
}
47+
return second.top();
48+
}
49+
void pop(void) {
50+
if (second.empty()) {
51+
channel(first, second);
52+
}
53+
second.pop();
54+
}
55+
```
56+
57+
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <stack>
2+
#include <iostream>
3+
using namespace std;
4+
class Queue {
5+
private:
6+
stack<int> first;
7+
stack<int> second;
8+
public:
9+
bool empty(void) {
10+
return first.empty() && second.empty();
11+
}
12+
void push(int x) {
13+
first.push(x);
14+
}
15+
int peek(void) {
16+
if (second.empty()) {
17+
channel(first, second);
18+
}
19+
return second.top();
20+
}
21+
void pop(void) {
22+
if (second.empty()) {
23+
channel(first, second);
24+
}
25+
second.pop();
26+
}
27+
private:
28+
void channel(stack<int> &src, stack<int> &dest) {
29+
while (!src.empty()) {
30+
dest.push(src.top());
31+
src.pop();
32+
}
33+
}
34+
};
35+
int main(int argc, char **argv)
36+
{
37+
Queue queue;
38+
queue.push(1);
39+
queue.push(2);
40+
queue.pop();
41+
cout << queue.peek() << endl;
42+
return 0;
43+
}

0 commit comments

Comments
 (0)