JavaScript实现双向链表的封装

function DoublyLinkedList(){
    //双向链表需要头指针尾指针
    this.head = null
    this.tail = null
    this.lenght = 0
    function Node(data){
        this.data = data;
        this.next = null;
        this.prev = null;
    }
    //向链表最后添加元素
    DoublyLinkedList.prototype.apped = function(element){
        let newNode = new Node(element)
        if(this.lenght === 0){
            this.head = newNode
            this.tail = newNode
        }else{
            newNode.prev= this.tail
            this.tail.next = newNode
            this.tail = newNode
        }
        this.lenght++
    }
    //重写toString方法
    DoublyLinkedList.prototype.toString = function(){
        return this.backwordString()
    }
    //逆向遍历链表
    DoublyLinkedList.prototype.forwordString = function(){
        let curred = this.tail
        let resultString = ""
        while(curred){
            resultString = resultString + curred.data + ' '
            curred = curred.prev
        }
        return resultString
    }
    //正向遍历链表
    DoublyLinkedList.prototype.backwordString = function(){
        let curred = this.head
        let resultString = ''
        while(curred){
            resultString = resultString + curred.data + ' '
            curred = curred.next
        }
        return resultString
    }
    //查找指定位置的元素
    DoublyLinkedList.prototype.get = function(position){
        let le = this.lenght / 2
        console.log(le);
        if(position < 0 || position >= this.lenght) return null
        if(position > le){
            let curred = this.tail
            let index = this.lenght - 1
            while(index-- > position){
                curred = curred.prev
            } 
            return curred.data
        }else{
            let curred = this.head
            let index = 0
            while(index++ < position){
                curred = curred.next
            }
            return curred.data
        }
    }
    //查看是否有某一元素
    DoublyLinkedList.prototype.indexOf = function(data){
        //从后往前找
        let curred = this.tail
        let index = 0
        while(curred){
            if(data === curred.data){
                return index
            }
            curred = curred.next
            index++
        }
        return -1
    }
    //修改某个位置的元素
    DoublyLinkedList.prototype.update = function(position,element){
        if(position < 0 || position >= this.lenght) return null
        let le = this.lenght / 2
        if(position > le){
            let curred = this.tail
            index = this.lenght - 1
            while(curred){
                if(index === position){
                    curred.data = element
                    return true 
                }
                curred = curred.prev
                index--
            }
        }else{
            let curred = this.head
            let index = 0
            while(curred){
                if(index === position){
                    curred.data = element
                    return true 
                }
                curred = curred.next
                index++
            }
        }
    }
    //移除特定位置的一项
    DoublyLinkedList.prototype.removeAt = function(position){
        if(position < 0 && position >= this.lenght) return null
        let le = this.lenght / 2
        if(position === 0){
            this.head = this.head.next
            this.head.next.prev = this.head
        }
        if(position > le){
            let curred = this.tail
            let index = this.lenght-1
            while(index-- > position){
                curred = curred.prev
            }
            curred.next.prev = curred.prev
            curred.prev.next = curred.next
        }else{
            let curred = this.head
            let index = 0
            while(index++ < position){
                curred = curred.next
            }
            curred.next.prev = curred.prev
            curred.prev.next = curred.next
        }
        this.lenght--
    }
    //移除指定数据
    DoublyLinkedList.prototype.remove = function(data){
        let curred = this.head
        while(curred){
            if(curred.data === data){
                curred.prev.next = curred.next
                curred.next.prev = curred.prev
                this.lenght--
                return true
            }
            curred = curred.next
        }
        return false
    }
    //判空
    DoublyLinkedList.prototype.isEmpty = function(){
        return !(this.lenght === 0)
    }
    //返回链表长度
    DoublyLinkedList.prototype.size = function(){
        return this.lenght
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值