Skip to content

Commit 4945e38

Browse files
committed
implement_stack_using_queues
1 parent 47f50e9 commit 4945e38

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ Golang solution for leetcode. For each problem, there is a simple *_test.go to t
181181
#### [222. Count Complete Tree Nodes](https://github.com/hitzzc/go-leetcode/tree/master/count_complete_tree_nodes)
182182
#### [223. Rectangle Area](https://github.com/hitzzc/go-leetcode/tree/master/rectangle_area)
183183
#### [224. Basic Calculator](https://github.com/hitzzc/go-leetcode/tree/master/basic_calculator)
184+
#### [225. Implement Stack using Queues](https://github.com/hitzzc/go-leetcode/tree/master/implement_stack_using_queues)
184185

185186

186187

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <queue>
2+
3+
class Stack {
4+
public:
5+
queue<int> q;
6+
// Push element x onto stack.
7+
void push(int x) {
8+
q.push(x);
9+
}
10+
11+
// Removes the element on top of the stack.
12+
void pop() {
13+
int num = q.size() - 1;
14+
while (num-- > 0){
15+
q.push(q.front());
16+
q.pop();
17+
}
18+
q.pop();
19+
}
20+
21+
// Get the top element.
22+
int top() {
23+
return q.back();
24+
}
25+
26+
// Return whether the stack is empty.
27+
bool empty() {
28+
return q.empty();
29+
}
30+
};

0 commit comments

Comments
 (0)