首页 > 编程语言 > 详细

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

时间:2020-06-23 12:30:50      阅读:74      评论:0      收藏:0      [点我收藏+]

使用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

 

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

原文:https://www.cnblogs.com/lanshanxiao/p/13181363.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!