3.栈和队列的实现(JavaScript版)

使用JavaScript实现 栈和队列

<!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.arr = [];
            this.push = function(value){
                this.arr.push(value);
            };
            this.pop = function(){
                return this.arr.pop();
            };
        }

        //封装一个队列
        function Queue(){
            this.arr = [];
            this.push = function(value){
                this.arr.push(value);
            };
            this.pop = function(){
                return this.arr.shift();
            };
        }

        var s = new Stack();
        s.push(1);
        s.push(2);
        s.push(3);
        console.log(s.pop());

        var q = new Queue();
        q.push(1);
        q.push(2);
        q.push(3);
        console.log(q.pop());
    </script>
</body>
</html>
栈和队列.html

猜你喜欢

转载自www.cnblogs.com/lanshanxiao/p/13181363.html