用两个栈实现队列

来源:互联网 发布:finale打谱软件 mac 编辑:程序博客网 时间:2024/05/08 05:47
  • 题目
    用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。

  • 代码

class Solution{public:    void push(int node) {        stack1.push(node);    }    int pop() {        if(stack2.empty()){            while(!stack1.empty()){            int len=stack1.size();            for(int i=0;i<len;i++){                int key=stack1.top();                stack1.pop();                stack2.push(key);            }          }        }        int key1=stack2.top();        stack2.pop();        return key1;    }private:    stack<int> stack1;    stack<int> stack2;};
0 0
原创粉丝点击