#include <iostream>
#include <queue>
using namespace std;
class Stack {
private:
queue<int> q1;
queue<int> q2;
public:
//判空
bool isEmpty() {
return q1.empty() && q2.empty();
}
//入栈
void S_push(int node) {
q1.push(node);
}
//出栈
int S_pop() {
if (isEmpty()) {
cout << "栈空!" << endl;
exit(1);
}
int node;
if (q1.size() == 1) {
node = q1.size();
q1.pop();
} else {
//将q1中元素入队q2直至q1剩下一个元素
while (!q1.empty()) {
q2.push(q1.front());
q1.pop();
if (q1.size() == 1) {
break;
}
//出队
node = q1.front();
q1.pop();
//将q2中元素全部入队q1
while (!q2.empty()) {
q1.push(q2.front());
q2.pop();
}
}
}
return node;
}
};
int main() {
//TODO 自己尝试写下测试用例
return 0;
}
队列模拟栈(C++)
最新推荐文章于 2024-06-04 15:06:52 发布