zoukankan      html  css  js  c++  java
  • 两个堆栈实现列队

     如何用两个堆栈模拟实现一个队列?

    如何用两个堆栈模拟实现一个队列?  如果这两个堆栈的容量分别是m和n(m>n),你的方法能保证的队列容量是多少?

    class Solution
    {   
         
        //主要思路:建立两个栈S1,S2,当执行PUSH操作时将数据压到S1中;当执行POP操作时分两种情况:
        //1、如果S2不为空,则从S2中弹出一个数并输出;2、如果S2为空S1不为空则将S1中的数据全部导入S2中,
        //如果两个都为空则表示队列中没有数字,输出-1。
         
    public:
        void push(int node) {
            stack1.push(node);
        }
     
        int pop() {
            if(stack2.empty())
                {
                while(!stack1.empty())
                    {
                    stack2.push(stack1.top());
                    stack1.pop();
                }
            }
            int p=stack2.top();
            stack2.pop();
            return p;
                 
        }
     
    private:
        stack<int> stack1;
        stack<int> stack2;
    };
  • 相关阅读:
    qiankun 报错:Target container with #container not existed while xxx mounting!
    promise加载队列实现
    promise 封装定时器
    关于promise
    节流防抖
    箭头函数特点
    this
    手写apply
    手写call
    手写bind函数
  • 原文地址:https://www.cnblogs.com/ranjiewen/p/6580363.html
Copyright © 2011-2022 走看看