栈的特性:先进后出
队列的特性:先进先出
这个原理就跟用两个杯子倒出沉淀物一样,先把 1 杯子上面的水倒入 2 杯子中,然后把 1 杯子的沉淀物取出,再把 2 杯子中的倒入到 1 中…依次循环
下面我们用代码实现:
#include <iostream>
#include <stack>
using namespace std;
class stackQueue {
stack<int>m_s1;
stack<int>m_s2;
public:
stackQueue() {
}
void push(int i) {
m_s1.push(i);
}
void pop() {
while (m_s1.size() > 1) {
m_s2.push(m_s1.top());
m_s1.pop();
}
m_s1.pop();
while (!m_s2.empty()) {
m_s1.push(m_s2.top());
m_s2.pop();
}
}
int front() {
while (m_s1.size() > 1) {
m_s2.push(m_s1.top());
m_s1.pop();
}
int tmp=m_s1.top();
while (!m_s2.empty()) {
m_s1.push(m_s2.top());
m_s2.pop();
}
return tmp;
}
int back() {
return m_s1.top();
}
};
int main() {
stackQueue sq;
sq.push(2);
sq.push(3);
sq.push(4);
cout << sq.front() << endl;
sq.pop();
cout << sq.front() << endl;
sq.pop();
cout << sq.front() << endl;
sq.pop();
system("pause");
return 0;
}