zoukankan      html  css  js  c++  java
  • js 数据结构-栈与队列

    /*
    【客栈的盘子/月井里的货物,后进先出】
    栈顶:最先入口/出口的位置
    栈底:最慢最晚出栈的位置
    */

    function Stack()
    {
        var item = [];
    
        //推(将货物推入月井)
        this.push = function(e)
        {
            item.push(e);
        }
    
        //弹(将月井最上方的货物搬出来)
        this.pop = function()
        {
            return item.pop();
        }
    
        //查(查看月井最上方的货物,但不搬出来)
        this.peek = function()
        {
            return item[item.length - 1];
        }
    
        //
        this.isEmpty = function()
        {
            return item.length == 0;
        }
    
        //
        this.size = function()
        {
            return item.length;
        }
    
        //
        this.clear = function()
        {
            item = [];
        }
    
        //
        this.price = function()
        {
            console.log(item.toString());
        }
    
        //
        this.toString = function()
        {
            return item.toString();
        }
    }

    十进制转二进制。任意进制转二进制

    /*
    十进制转二进制
    原理是将一个大于0的整数,每次都除以2并且记录余数,直到余数为0为止。然后将记录余数叠加即为二进制
    */
    function divideBy2(decNumber)
    {
        var remStack = new Stack();
        var rem;
        var binaryString;
    
        while(decNumber > 0)
        {
            rem = Math.floor(decNumber % 2);
            remStack.push(rem);
            decNumber = Math.floor(decNumber / 2);
        }
    
        if(!remStack.isEmpty()) return remStack.toString();
    }
    
    
    function baseConverter(decNumer,base)
    {
        var remStack = new Stack();
        var rem;
        var binaryString;
        var digits = '0123456789ABCDEF';
    
        while(decNumber > 0)
        {
            rem = Math.floor(decNumber % base);
            remStack.push(rem);
            decNumber = Math.floor(decNumber / base);
        }
    
        if(!remStack.isEmpty()) 
        {
            binaryString += digits[remStack.pop()];
        }
    
        return binaryString;
    }

    队列

    /*
    队列,先进先出
    */
    function Queue()
    {
        var item = [];
    
        //
        this.enqueue = function(e)
        {
            item.push(e);
        }
    
        //shift
        this.dequeue = function()
        {
            return item.shift();
        }
    
        //
        this.front = function()
        {
            return item[0];
        }
    
        //
        this.isEmpty = function()
        {
            return item.length == 0;
        }
    
        //
        this.size = function()
        {
            return item.length;
        }
    
        //
        this.clear = function()
        {
            item = [];
        }
    
        //
        this.price = function()
        {
            console.log(item.toString());
        }
    
        //
        this.toString = function()
        {
            return item.toString();
        }
    }
  • 相关阅读:
    datatables 自定义排序
    nginx 高并发配置参数
    查看域名https证书到期时间
    Python七大原则,24种设计模式
    scrapy RuntimeError: maximum recursion depth exceeded while calling a Python object 超出python最大递归数异常
    scrapy 用pycharm调试
    修改Mysql 字符集,支持Emoji表情
    redis资料
    mysql 开启日志与性能调优
    python 版本号比较 重载运算符
  • 原文地址:https://www.cnblogs.com/CyLee/p/5628968.html
Copyright © 2011-2022 走看看