<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
        問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
        當前位置: 首頁 - 科技 - 知識百科 - 正文

        js單向鏈表的具體實現實例_javascript技巧

        來源:懂視網 責編:小采 時間:2020-11-27 21:10:19
        文檔

        js單向鏈表的具體實現實例_javascript技巧

        js單向鏈表的具體實現實例_javascript技巧: 代碼如下:function linkNode(_key, _value){ /// /// 鏈表類的節點類 /// this.Key = _key; this.Value = _value; this.next = null;}function Link(){ /// /// 創建一個鏈表類 /// this.root = new li
        推薦度:
        導讀js單向鏈表的具體實現實例_javascript技巧: 代碼如下:function linkNode(_key, _value){ /// /// 鏈表類的節點類 /// this.Key = _key; this.Value = _value; this.next = null;}function Link(){ /// /// 創建一個鏈表類 /// this.root = new li

        代碼如下:
        function linkNode(_key, _value)
        {
        ///
        /// 鏈表類的節點類
        ///

        this.Key = _key;
        this.Value = _value;
        this.next = null;
        }
        function Link()
        {
        ///
        /// 創建一個鏈表類
        ///

        this.root = new linkNode(null, null); //root永遠是個空節點
        this.end = this.root;
        }
        Link.prototype =
        {
        count: 0,
        value: function (_key)
        {
        ///
        /// 根據key的值來獲取value值
        ///

        ///
        /// key的值
        ///
        ///
        /// 對應的value的值
        ///

        var i = this.root;
        while (Boolean(i = i.next))
        {
        if (i.Key == _key)
        return i.Value;
        }
        },
        add: function (_key, _value)
        {
        ///
        /// 往鏈表的尾部中加入一個節點
        ///

        ///
        /// key的值
        ///
        ///
        /// value的值
        ///
        ///
        /// 返回新增加的value的值
        ///

        var i = this.root;
        while (Boolean(i = i.next))
        {
        if (i.Key == _key)
        return i.Value = _value;
        }
        var node = new linkNode(_key, _value);
        if (this.count == 0)
        this.root.next = node;
        else
        this.end.next = node;
        this.end = node;
        this.count++;
        return _value;
        },
        insert: function (_key, node)
        {
        ///
        /// 從鏈表類的某節點之后插入新節點node.
        ///

        ///
        /// 在鍵值等于_key的元素之后插入
        ///
        ///
        /// 要插入的元素
        ///
        var i = this.root;
        while (Boolean(i = i.next))
        {
        if (i.Key == _key)
        {
        var tmp = i.next;
        i.next = node;
        node.next = tmp;
        break;
        }
        }
        },
        insertBefore: function (_key, node)
        {
        ///
        /// 從鏈表類的某節點之后插入新節點node.
        ///

        ///
        /// 在鍵值等于_key的元素之后插入
        ///
        ///
        /// 要插入的元素 www.gxlcms.com
        ///
        var i = this.root;
        while (Boolean(i = i.next))
        {
        if (i.next.Key == _key)
        {
        var tmp = i.next;
        i.next = node;
        node.next = tmp;
        break;
        }
        }
        },
        remove: function (_key)
        {
        ///
        /// 從鏈表類中移除一個key
        ///

        ///
        /// key的值
        ///
        var i = this.root;
        do
        {
        if (i.next.Key == _key)
        {
        if (i.next.next == null)
        this.end = i;
        i.next = i.next.next;

        this.count--;
        return;
        }
        } while (Boolean(i = i.next))
        },
        exists: function (_key)
        {
        ///


        /// 檢查鏈表類中是否存在一個key
        ///

        ///
        /// key的值
        ///
        ///
        ///

        var i = this.root;
        while (Boolean(i = i.next))
        if (i.Key == _key)
        return true;
        return false;
        },
        removeAll: function ()
        {
        ///
        /// 清空鏈表類
        ///

        this.root = new linkNode(null, null);
        this.end = this.root;
        this.count = 0;
        },
        Obj2str: function (o)
        {
        if (o == undefined)
        {
        return "";
        }
        var r = [];
        if (typeof o == "string")
        return "\"" + o.replace(/([\"\\])/g, "\\$1").replace(/(\n)/g, "\\n").replace(/(\r)/g, "\\r").replace(/(\t)/g, "\\t") + "\"";
        if (typeof o == "object")
        {
        if (!o.sort)
        {
        for (var i in o)
        r.push("\"" + i + "\":" + this.Obj2str(o[i]));
        r = "{" + r.join() + "}";
        }
        else
        {
        for (var i = 0; i < o.length; i++)
        r.push(this.Obj2str(o[i]))
        r = "[" + r.join() + "]";
        }
        return r;
        }
        return o.toString().replace(/\"\:/g, '":""');
        },
        getJSON: function ()
        {
        ///
        /// 轉換成JSON字符串
        ///

        ///
        ///

        //內部方法,用于遞歸
        var me = this;
        var getChild = function (node)
        {
        var str = "";
        str += "{\"Key\":\"" + node.Key + "\",\"Value\":" + me.Obj2str(node.Value);
        if (node.next != null)
        str += ",\"next\":" + getChild(node.next);
        else
        str += ",\"next\":\"null\"";
        str += "}";
        return str;
        };
        var link = "{\"root\":{\"Key\":\"null\",\"Value\":\"null\",\"next\":";
        if (this.count == 0)//如果空表
        {
        return "{\"root\":{\"Key\":\"null\",\"Value\":\"null\",\"next\":\"null\"},\"end\":{\"Key\":\"null\",\"Value\":\"null\",\"next\":\"null\"},\"count\":\"0\"}";
        }
        link += getChild(this.root.next) + "}";
        //加上end
        link += ",\"end\":{\"Key\":\"" + this.end.Key + "\",\"Value\":" + me.Obj2str(this.end.Value) + ",\"next\":\"null\"";
        link += "},\"count\":\"" + this.count + "\"}";
        return link;
        },
        getArrayJSON: function ()
        {
        ///
        /// 轉所有節點的value換成JSON字符串,數組格式
        ///

        ///
        ///

        var link = "{\"link\":[";
        var i = this.root;
        while (Boolean(i = i.next))
        {
        link += this.Obj2str(i.Value) + ",";
        }
        link = link.substr(0, link.length - 1);
        link += "]}";
        return link;
        },
        sort: function (fn)
        {
        ///
        /// 對鏈表進行排序
        ///

        ///
        /// 比較兩個鏈表元素大小的方法,當返回真時,此方法的參數所指的節點將往下沉
        ///
        if (fn != null)
        {

        var i = this.root;
        while (Boolean(i = i.next))
        {
        var j = this.root;
        while (Boolean(j = j.next))
        {
        if (j.next != null)
        {
        if (fn.call(this, j))
        {
        var Key = j.Key;
        var Value = j.Value;
        j.Key = j.next.Key;
        j.Value = j.next.Value;
        j.next.Key = Key;
        j.next.Value = Value;
        }
        }
        }
        this.end = i;
        }

        }
        else
        {

        }
        }
        };

        聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

        文檔

        js單向鏈表的具體實現實例_javascript技巧

        js單向鏈表的具體實現實例_javascript技巧: 代碼如下:function linkNode(_key, _value){ /// /// 鏈表類的節點類 /// this.Key = _key; this.Value = _value; this.next = null;}function Link(){ /// /// 創建一個鏈表類 /// this.root = new li
        推薦度:
        標簽: 技巧 主要 實現
        • 熱門焦點

        最新推薦

        猜你喜歡

        熱門推薦

        專題
        Top
        主站蜘蛛池模板: 91视频精品全国免费观看| 亚洲经典千人经典日产| 免费无遮挡无码视频在线观看 | 亚洲自偷自拍另类图片二区| 精品一区二区三区免费观看| 久久精品国产亚洲5555| 国产99久久久国产精免费| 国外亚洲成AV人片在线观看| XXX2高清在线观看免费视频| 久久久久久久综合日本亚洲| 国产精品免费高清在线观看| 午夜亚洲AV日韩AV无码大全| 99免费视频观看| 亚洲成A∨人片在线观看无码| 国产免费的野战视频| 亚洲熟妇无码八V在线播放| 国产精品色午夜视频免费看| 国产成人精品亚洲一区| 亚洲精品国自产拍在线观看| 国产久爱免费精品视频| 亚洲AV无码码潮喷在线观看| 91精品国产免费| 亚洲暴爽av人人爽日日碰| 亚洲AV之男人的天堂| 国产又黄又爽又大的免费视频| 久久噜噜噜久久亚洲va久| 在线视频精品免费| 色一情一乱一伦一视频免费看| 久久夜色精品国产亚洲av| 一级毛片在线免费观看| 亚洲色中文字幕在线播放| 亚洲人成网站色在线入口| 男人进去女人爽免费视频国产 | 中文在线观看免费网站| 亚洲国产精品第一区二区| 久久久久国色AV免费看图片| 一级特黄aaa大片免费看| 亚洲最大的成网4438| 四虎永久在线精品视频免费观看| 一级有奶水毛片免费看| 色在线亚洲视频www|