双向链表的封装

function DoublyLinkedList(){
    function Node(data){
        this.data = data
        this.prev = null
        this.next = null
    }
    this.head = null
    this.tail = null
    this.length = 0
    //append方法
    DoublyLinkedList.prototype.append = function(data){
        //情况1:第一个元素    情况2:第1- 个元素
        let newNode = new Node(data)
        if(!this.length){
            this.head = newNode
            this.tail = newNode
        }else{
        
            newNode.prev = this.tail
            this.tail.next = newNode
            this.tail = newNode
        }
        this.length++
    }
    //tostring方法
    DoublyLinkedList.prototype.toString = function(){
        return this.backwardString()
    }
    //forwardString方法
    DoublyLinkedList.prototype.forwardString = function(){
        let current = this.tail
        let resultStr = ''
        while(current){
            resultStr += current.data+' '
            current = current.prev
        }
        return resultStr
    }
    //backwardString方法
    DoublyLinkedList.prototype.backwardString = function(){
        let current = this.head
        let resultStr = ''
        while(current){
            resultStr +=current.data+' '
            current = current.next
        }
        return resultStr
    }
    //insert方法
    DoublyLinkedList.prototype.insert = function(position,data){
        // 越界检查   链表为空  插入位置为0  插入位置为last  插入位置为中间
        if(position < 0 || position > this.length)  return false
        let newNode = new Node(data)
        if(!this.length){
            this.head = newNode
            this.tail = newNode
        }else if(position == 0){
            this.head.prev = newNode
            newNode.next = this.head
            this.head = newNode
        }else if(position == this.length){
            newNode.prev = this.tail
            this.tail.next = newNode
            this.tail = newNode
        }else{
            let index = 0
            let current = this.head
            while(index++ < position){
                current = current.next
            }
            newNode.prev = current.prev
            newNode.next = current
            current.prev.next = newNode
            current.prev = newNode
        }

        this.length++
    }
    //get方法
    DoublyLinkedList.prototype.get = function(position){
        //越界检查   获取元素  position较小,从前往后   position较大,从后往前
        if(position < 0 || position >=this.length)  return null
        if(this.length/2 > position){
            let current = this.head
            let index = 0
            while(index++ < position){
                current = current.next
            }
            return current.data
        }else{
            let current = this.tail
            let index = this.length-1
            while(index-- > position){
                current = current.prev
            }
            return current.data
        }
    }
    //indexOf方法
    DoublyLinkedList.prototype.indexOf = function(data){
        //定义变量  遍历链表  
        let current = this.head
        let index = 0
        while(current){
            if(current.data == data){
                return index
            }
            current = current.next
            index++
        }
        return -1
    }
    //update方法
    DoublyLinkedList.prototype.update = function(position,data){
        //越界检查  找到节点  修改节点
        if(position <0 || position >= this.length) return false
        let current = this.head
        let index = 0
        while(index++ < position){
            current = current.next
        }
       current.data = data
        return true
    }
    //removeAt方法
    DoublyLinkedList.prototype.removeAt = function(position){
        //越界检查   只有一个节点   position=0  position=this.length  position= 1-
        if(position<0 || position>=this.length)  return null
        let current = this.head
        if(this.length==1){
            this.head = null
            this.tail = null
        }else if(position == 0){
            //删除 指向删除节点的引用
            this.head.next.prev = null
            this.head = this.head.next
        }else if(position == this.length-1){
            current = this.tail
            this.tail.prev.next = null
            this.tail = this.tail.prev
        }else{
            let index = 0
            let current = this.head
            while(index++ < position){
                current = current.next
            }
            current.prev.next = current.next
            current.next.prev = current.prev
            this.length--
            return current.data
        }
        this.length--
        return current.data
    }
    //remove方法
    DoublyLinkedList.prototype.remove = function(data){
        let index = this.indexOf(data)
        return this.removeAt(index)
    }
    //isEmpty方法
    DoublyLinkedList.prototype.isEmpty = function(){
        return this.length==0
    }
    //size方法
    DoublyLinkedList.prototype.size =function(){
        return this.length
    }
    //获取链表第一个元素的data
    DoublyLinkedList.prototype.getHead = function(){
        return this.head.data
    }
    //获取链表最后一个元素的data
    DoublyLinkedList.prototype.getTail = function(){
        return this.tail.data
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值