js封装链表操作

1 篇文章 0 订阅
  1. 前端常用的数据结构就是数组 因为前端的数组封装了很多方法:增删改查 以及各种循环操作,但是数组的添加,删除在改变索引的情况下效率不高,数组是一块连续的内存空间,如果数据超过初始定义的长度再往里添加数据的话要扩容效率不高,数组的优势在于可以根据索引查找数据效率很快(所以在查找方面前端可以用对象的方式去查找,和数组索引一样,obj[key] )。
  2. 链表是一种非连续非顺序的存储结构:每一项包括两部分,当前项的值,以及指向下一个项的指针,链表对于插入删除操作效率比数组高

实现方式

class ListSelf {
        head = null
        length = 0
        CreactNode = class {
            constructor(data = '', next = null) {
                this.data = data
                this.next = next
            }
        }
        append(data) {
            const newNode = new this.CreactNode(data)
            // 插入第一个数据 
            if (this.length === 0) this.head = newNode
            else {
                // 不是第一次插入
                let current = this.head
                while (current.next) current = current.next
                current.next = newNode
            }
            return this.length += 1
        }
        insert(data, insertIndex) {
            if (insertIndex < 0 || insertIndex > this.length) return false
            const newNode = new this.CreactNode(data)
            this.length += 1
            // 插入第一个位置
            if (insertIndex === 0) {
                newNode.next = this.head
                this.head = newNode
                return true
            }
            // 插入其他位置
            let current = this.head,
                preItem = null,
                index = 0;
            while (index++ < insertIndex) {
                preItem = current
                current = current.next
            }
            // 插入到位置的前面(js数组 splice的插入也是这个规则:
            // 例如插入位置为2 则插入的值在2的位置 之前的2位置往后移)
            newNode.next = current
            preItem.next = newNode
            // 插入到位置的后面
            // newNode.next = current.next
            // current.next = newNode
            return true
        }
        removeAt(removeIndex) {
            if (removeIndex < 0 || removeIndex >= this.length) return false
            this.length -= 1
            if (removeIndex === 0) return this.head = this.head.next
            let current = this.head,
                preItem = null,
                index = 0;
            while (index++ < removeIndex) {
                preItem = current
                current = current.next
            }
            return preItem.next = current.next
        }
        remove(data) {
            const index = this.indexOf(data)
            return this.removeAt(index)
            // let current = this.head,
            //     preItem = null;
            // while (current) {
            //     if (JSON.stringify(data) === JSON.stringify(current.data)) {
            //         if (preItem) preItem.next = current.next
            //         else this.head = current.next
            //         return this.length -= 1
            //     }
            //     preItem = current
            //     current = current.next
            // }
            // return true
        }
        update(data, updateIndex) {
            if (updateIndex < 0 || updateIndex >= this.length) return false
            let current = this.head,
                index = 0;
            while (index++ < updateIndex) {
                current = current.next
            }
            return current.data = data
        }
        get(getIndex) {
            if (getIndex < 0 || getIndex >= this.length) return false
            let current = this.head,
                index = 0;
            while (index++ < getIndex) {
                current = current.next
            }
            return current.data
        }
        indexOf(data) {
            let current = this.head,
                index = 0;
            while (current) {
                if (JSON.stringify(current.data) === JSON.stringify(data)) return index
                current = current.next
                index += 1
            }
            return -1
        }
        toStringOrArray(type = 'string', placeholder = ' ') {
            let current = this.head,
                listString = '';
            if (type === 'array') listString = []
            while (current) {
                if (type === 'string') listString += current.data + placeholder
                else listString.push(current.data)
                current = current.next
            }
            return listString
        }
        isEmpty() { return this.length === 0 }
        size() { return this.length }
    }
    const asd = new ListSelf()
    asd.append(0)
    asd.append(1)
    asd.append(2)
    asd.append(3)
    // console.log(asd.toStringOrArray('string'))
    // asd.insert('zzz', 2)
    // console.log(asd.toStringOrArray('string'))
    // console.log(asd.get(2))
    // console.log(asd.indexOf(9))
    // asd.update(123, 0)

    asd.remove(90)
    console.log(asd.toStringOrArray('string'))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值