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();
        }
    }
  • 相关阅读:
    MySql模糊查询like通配符使用详细介绍
    使用powershell批量添加Qt的文件(生成pro)
    宏定义的教训
    使用powershell批量添加Keil和IAR的头文件路径
    python和数据科学(Anaconda)
    CDCE913产生任意频率
    QT中检索设定目录下所有指定文件的方法
    QT中将ASCII转换为对应数值的方法
    STM8如何使用自带的bootloader
    QT中使用函数指针
  • 原文地址:https://www.cnblogs.com/CyLee/p/5628968.html
Copyright © 2011-2022 走看看