#include <iostream>
#include <stack>
using namespace std;
class Queue {
public:
Queue() {}
void push(int x) {
stIn.push(x);
}
int pop() {
if (stOut.empty()) {
while(!stIn.empty()) {
stOut.push(stIn.top());
stIn.pop();
}
}
int result = stOut.top();
stOut.pop();
return result;
}
int peek() {
int res = this->pop();
stOut.push(res);
return res;
}
bool empty() {
return stIn.empty() && stOut.empty();
}
public:
stack<int> stIn;
stack<int> stOut;
};
int main() {
Queue Q;
Q.push(1);
Q.push(2);
Q.push(3);
while(!Q.empty()) {
cout << Q.pop() << " " << endl;
}
return 0;
}
C++ 使用栈模拟队列
最新推荐文章于 2024-07-04 17:35:22 发布