首页 > 编程语言 >JavaScript-栈结构

JavaScript-栈结构

时间:2022-10-09 23:11:17浏览次数:56  
标签:function return items JavaScript push prototype Stack 结构

 

1.认识栈结构

 

后进先出 

 

 

 

 

 

 

2.栈的面试题

 

 

 不是一次性进栈,是可以一边进一边出的

 

 

3.栈结构的封装

 

 操作

 

 

 

封装

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    
    <script>
        // 封装栈类
        function Stack(){
            // 栈的属性
            this.items=[];


            // 栈的相关操作
            // 1.将元素压入栈
            // this.push=function(){
                
            // }
            // 另一种写法
            Stack.prototype.push=function(element){
                this.items.push(element)
            }


            // 2.从栈中取出元素
            Stack.prototype.pop=function(){
                return this.items.pop()
            }

            // 3.查看一下栈顶元素
            Stack.prototype.peek=function(){
                return this.items[this.items.length-1]
            }

            // 4.判断栈是否为空
            Stack.prototype.isEmpty=function(){
                return this.items.length==0;
            }


            // 5.获取栈元素的个数
            Stack.prototype.size=function(){
                return this.items.length;
            }

            // 6.toString方法
            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(20)
        s.push(10)
        s.push(100)
        s.push(77)
        alert(s)

        s.pop()
        s.pop()
        alert(s)

        alert(s.peek())

        alert(s.isEmpty())

        alert(s.size())

    </script>
</body>
</html>

 

 

 

 

 

4.十进制转二进制

 

 

 

 例子

 

例子

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    
    <script>
        // 封装栈类
        function Stack(){
            // 栈的属性
            this.items=[];


            // 栈的相关操作
            // 1.将元素压入栈
            // this.push=function(){
                
            // }
            // 另一种写法
            Stack.prototype.push=function(element){
                this.items.push(element)
            }


            // 2.从栈中取出元素
            Stack.prototype.pop=function(){
                return this.items.pop()
            }

            // 3.查看一下栈顶元素
            Stack.prototype.peek=function(){
                return this.items[this.items.length-1]
            }

            // 4.判断栈是否为空
            Stack.prototype.isEmpty=function(){
                return this.items.length==0;
            }


            // 5.获取栈元素的个数
            Stack.prototype.size=function(){
                return this.items.length;
            }

            // 6.toString方法
            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(20)
        // s.push(10)
        // s.push(100)
        // s.push(77)
        // alert(s)

        // s.pop()
        // s.pop()
        // alert(s)

        // alert(s.peek())

        // alert(s.isEmpty())

        // alert(s.size())


        // 函数:将十进制转成二进制
        function dec2bin(decNumber){
            var stack = new Stack()
            // 循环操作
            while(decNumber > 0){
                // 2.1获取会数,并且放入到栈
                stack.push(decNumber % 2)
                // 2.2获取整除后的结果,作为下一次运行的数字
                decNumber=Math.floor(decNumber / 2)
            }

            // 3.从栈中取出0和1
            var binaryString=''
            while(!stack.isEmpty()){
                binaryString+=stack.pop()
            }
            return binaryString;
        }
       
        // 测试10转2
        alert(dec2bin(100))


    </script>
</body>
</html>

 

标签:function,return,items,JavaScript,push,prototype,Stack,结构
From: https://www.cnblogs.com/hechunfeng/p/16767781.html

相关文章