javascript - js链表的理解
巴扎黑
巴扎黑 2017-04-11 12:03:27
[JavaScript讨论组]
      function LinkedList() {
            let Node = function (ele) {
                this.ele = ele
                this.next = null
            },
                length = 0, head = null;
            this.append = function (ele) {
                var node = new Node(ele), //{1}         
                    current; //{2} 
                if (head === null) { //列表中第一个节点 //{3}         
                    head = node;
                } else {
                    current = head; //{4} 
                    //循环列表,直到找到最后一项         
                    while (current.next) {
                        current = current.next;
                    }
                    //找到最后一项,将其next赋为node,建立链接         
                    current.next = node; //{5}     
                }
                length++; //更新列表的长度 //{6} 
            };
           this.print = function () {
            var current = head, //{1}         
                string = '';    //{2} 

            while (current) {   //{3}         
                string += current.ele; //{4}         
                current = current.next;   //{5}     
            }
            return string;                //{6} 
        };
    }
    let LinkedList1= new LinkedList();
    [1,2,3,4,5,6].forEach(function (value) {
        LinkedList1.append(value)
    })
    console.log(LinkedList1.print(); //123456

这样实现一个链表的话,每一个项存在哪个函数的作用域?如果是append这个函数的作用域,print查找的时候非父级作用域的变量可以读取吗?

巴扎黑
巴扎黑

全部回复(0)
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号