queue_use_stack
queue_use_stack.cpp 源码
// 使用栈实现队列
#include "stack"
#include "iostream"
using namespace std;
class MyQueue {
private:
stack<int> output;
stack<int> input;
public:
MyQueue() {
}
void push(int x) {
input.push(x);
}
int pop() {
int res = peek();
output.pop();
return res;
}
int peek() {
if (output.empty()) {
while (!input.empty()) {
output.push(input.top());
input.pop();
}
}
int res = output.top();
return res;
}
bool empty() {
return input.empty() && output.empty();
}
};
/**
* Your MyQueue object will be instantiated and called as such:
* MyQueue* obj = new MyQueue();
* obj->push(x);
* int param_2 = obj->pop();
* int param_3 = obj->peek();
* bool param_4 = obj->empty();
*/
你可能感兴趣的文章
0
赞
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦