• 栈结构数组的实现


    栈结构数组的实现

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
    </head>
    <body>
        <script>
         function Stack(){
            this.items = [] 
            // 栈的相关操作
            // 将元素压入
            Stack.prototype.push = function(element){
                this.items.push(element)
            }
            Stack.prototype.pop = function(){
                return this.items.pop()
            }
            Stack.prototype.peek = function(){
                return this.items[this.items.length-1]
            }
            Stack.prototype.isEmpty= function(){
                return this.items.length == 0
            }
            Stack.prototype.size = function(){
                return this.items.length
            }
            Stack.prototype.toString = function(){
                var resultString = ''
                for(var i=0;i<this.items.length;i++){
                    resultString+=this.items[i]+''
                }
                return resultString
            }
    
    
         }
         var s = new Stack()
         s.push(10)
         s.push(20)
         alert(s)
         s.pop(10)
         s.push(50)
         s.push(100)
         alert(s.peek())
         alert(s.isEmpty())
         alert(s.size())
         alert(s.toString())
        </script>
    </body>
    </html>
    

    栈的应用,十进制转二进制
    二进制在计算机中非常重要,计算机里所有内容都是二进制数字表示的
    10进制转2进制方法

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
    </head>
    <body>
        <script>
        function Stack(){
            this.items = [] 
            // 栈的相关操作
            // 将元素压入
            Stack.prototype.push = function(element){
                this.items.push(element)
            }
            Stack.prototype.pop = function(){
                return this.items.pop()
            }
            Stack.prototype.peek = function(){
                return this.items[this.items.length-1]
            }
            Stack.prototype.isEmpty= function(){
                return this.items.length == 0
            }
            Stack.prototype.size = function(){
                return this.items.length
            }
            Stack.prototype.toString = function(){
                var resultString = ''
                for(var i=0;i<this.items.length;i++){
                    resultString+=this.items[i]+''
                }
                return resultString
            }
    
    
         }
            function dec2bin(decNumber){
                var stack = new Stack()
                while(decNumber>0){
                    stack.push(decNumber%2)
                    // 获取整除后的结果作为下一次运算的数字
                    decNumber= Math.floor(decNumber/2)
                }
                // 从栈中取出0和1
                var binaryString = ''
                while(!stack.isEmpty()){
                    binaryString+=stack.pop()
                }
                return binaryString
            }
            alert(dec2bin(100))
            alert(dec2bin(10))
            alert(dec2bin(1000))
        </script>
    </body>
    </html>
    
  • 相关阅读:
    poj1088滑雪
    百度关键词质量度如何提高?9个质量度影响因素
    百度竞价点击价格怎么算?计算公式准吗
    什么是百度竞价包年?竞价包年骗局揭露
    百度竞价如何调价?不是你想的那么简单
    为什么我的关键词搜不到,没有排名?
    为什么你的百度竞价有点击无咨询
    百度竞价中那些不为人知的规则与猫腻
    百度推广关键词点击价格为什么会高于出价?因为你开通了这8项功能
    百度推广点击价格怎样降低,怎样省钱?
  • 原文地址:https://www.cnblogs.com/smart-girl/p/13440590.html
Copyright © 2020-2023  润新知