题解 | #用两个栈实现队列#
用两个栈实现队列
https://www.nowcoder.com/practice/54275ddae22f475981afa2244dd448c6
#include <cstdio> class Solution { public: void push(int node) { stack1.push(node); /// 把栈 1 当做入栈 ,专门做入栈操作 } int pop() { if(stack2.empty()) // Stack2作专门的出栈, 如果 Stack2 没有数据了, 就把Stack1全部转移过来, { while(!stack1.empty()){ stack2.push(stack1.top()); stack1.pop(); } } int ret = stack2.top(); stack2.pop(); return ret; } private: stack<int> stack1; stack<int> stack2; };