js實(shí)現(xiàn)鏈表結(jié)構(gòu)

鏈表

React源碼中使用了鏈表的結(jié)構(gòu)來(lái)存儲(chǔ),今天使用js實(shí)現(xiàn)一下
什么是鏈表?
綠皮火車車箱,類型地下黨,只知上級(jí)下級(jí),不知其他

鏈表的優(yōu)點(diǎn):是存儲(chǔ)動(dòng)態(tài)靈活,可隨機(jī)存儲(chǔ), 不像數(shù)組在內(nèi)存中存儲(chǔ)需要連續(xù)的空間
特性:內(nèi)存不連續(xù)性和無(wú)索引
讀?。篛(n), 插入/刪除:O(1),適用于插入和刪除較多的場(chǎng)景

代碼實(shí)現(xiàn):
var Node = function(ele) {
            this.ele = ele
            this.next = null
        }
        function LinkList(){
            this.length = 0
            this.head = null
        }
        /**添加*/
        LinkList.prototype.append = function(ele) {
                var node = new Node(ele)
                var current // 找到尾節(jié)點(diǎn)即node.next為null時(shí)

                if(this.head === null) {
                    this.head = node
                }else {
                    current = this.head
                    //  找到尾節(jié)點(diǎn)
                    while(current.next) {
                        current = current.next
                    }
                    current.next = node
                }
                this.length++
            }
        /**插入*/
        LinkList.prototype.insert = function(pos, ele) {
                if(pos > -1 && pos <= this.length) {
                    var node = new Node(ele)

                    var current = this.head
                    var index = 0
                    var previous
                    if(pos === 0) {
                        node.next = current
                        this.head = node
                    }else{
                        // 從head開(kāi)始 循環(huán)遍歷依次向下查找,
                        // 找到pos位置的前一個(gè)節(jié)點(diǎn)和當(dāng)前節(jié)點(diǎn)
                        while(index++ < pos){
                            previous = current
                            current = current.next
                        }
                        previous.next = node
                        node.next = current
                    }
                    this.length++
                    return true
                }else{
                    return false
                }
            }

        /**根據(jù)pos刪除*/    
        LinkList.prototype.removeByPos = function(pos) {
                if(pos > -1 && pos < this.length) {
                    var current = this.head
                    var index = 0
                    var previous
                    if(pos == 0) {
                        this.head = current.next
                    }else {
                        // 從head開(kāi)始 循環(huán)遍歷依次向下查找,
                        // 找到pos位置的前一個(gè)節(jié)點(diǎn)和當(dāng)前節(jié)點(diǎn)
                        while(index++ < pos) {
                            previous = current
                            current = current.next
                        }
                        previous.next = current.next
                    }
                    this.length--
                    return current.ele
                }else {
                    return null
                }
            }       

        /**返回位置*/
        LinkList.prototype.indexOf = function(ele){
                var current = this.head
                var index = 0
                while(current){
                    if(ele === current.ele){
                        return index
                    }
                    index++
                    current = current.next
                }
                return -1
            }

        var link = new LinkList()
        link.append('hell') 
        link.append('yoyo')
        link.insert(0, 'first')
        console.log(link)
        // {
        //   "length": 3,
        //   "head": {
        //     "ele": "first",
        //     "next": {
        //       "ele": "hell",
        //       "next": {
        //         "ele": "yoyo",
        //         "next": null
        //       }
        //     }
        //   }
        // }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容