文章目錄
兩個棧設計隊列
- push操作就直接往stack1中push
- pop操作需要分類一下:如果stack2爲空,那麼需要將stack1中的數據轉移到stack2中,然後在對stack2進行pop;如果stack2不爲空,直接pop
class Solution
{
public:
void push(int node) {
stack1.push(node);
}
int pop() {
if(stack2.empty()){
while(not stack1.empty()){
temp = stack1.top();
stack1.pop();
stack2.push(temp);
}
}
temp = stack2.top();
stack2.pop();
return temp;
}
private:
int temp;
stack<int> stack1; // 入隊
stack<int> stack2; // 出隊
};