剑指offer 面试题:两个栈实现一个队列

题目:用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。

思路:两个栈s1和s2,入栈只往s1中入,出栈从s2中出,若出的时候s2空了,将s1所有元素放入s2中。这里就能通过提交。其实这里还要有  更详细的 判断,比如往s1中插入的时候 如果s1满了怎么办,这里没写

class Solution
{
public:
    void push(int node) {
        stack1.push(node);
    }
 
    int pop() {
        if(stack2.empty())
        {
            while(!stack1.empty())
            {
                int a = stack1.top();
                stack1.pop();
                stack2.push(a);
            }
        }
        int res = stack2.top();
        stack2.pop();
        return res;
    }
 
private:
    stack<int> stack1;
    stack<int> stack2;
};

猜你喜欢

转载自blog.csdn.net/qq_21997625/article/details/88088230