mirror of
https://github.com/chefyuan/algorithm-base.git
synced 2025-08-05 23:21:38 +00:00
栈和队列专题更新cpp代码
This commit is contained in:
@@ -18,6 +18,8 @@
|
||||
|
||||
下面我们来看一下题目代码,也是很容易理解。
|
||||
|
||||
#### 题目代码
|
||||
|
||||
Java Code:
|
||||
```java
|
||||
class MyStack {
|
||||
@@ -87,3 +89,32 @@ MyStack.prototype.empty = function() {
|
||||
};
|
||||
```
|
||||
|
||||
C++ Code:
|
||||
|
||||
```cpp
|
||||
class MyStack {
|
||||
queue <int> q;
|
||||
public:
|
||||
void push(int x) {
|
||||
q.push(x);
|
||||
for(int i = 1;i < q.size();i++){
|
||||
int val = q.front();
|
||||
q.push(val);
|
||||
q.pop();
|
||||
}
|
||||
}
|
||||
|
||||
int pop() {
|
||||
int val = q.front();
|
||||
q.pop();
|
||||
return val;
|
||||
}
|
||||
int top() {
|
||||
return q.front();
|
||||
}
|
||||
bool empty() {
|
||||
return q.empty();
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
|
Reference in New Issue
Block a user