双向链表实现查询、删除、插入、末尾增加

双向链表可以从前往后查询或者从后往前查询,提高了查询效率。需要存储两个方向的指针,占用了更大的空间。

let DoubleNode = function(element){
    this.element = element;
    this.next = null;
    this.prior = null;
}
class DobuleLinkedList {
    constructor() {
        this.length = 0;
        this.head = null; 
        this.tail = null;
    }
    // 获取某位置的结点
    getElementAt(position) {
        if(position < 0 || position >= this.length){
            return;
        }
        let current;
        if(position <= this.length/2){
            current = this.head;
            for(let i = 0; i < position; i++){
                current = current.next;  
            }
        }else{
            current = this.tail;
            for(let i = this.length - 1; i > position; i++){
                current = current.prior;
            }
        }


        return current;
    }
    append(element) {
        let node = new DoubleNode(element);
        if(this.head === null){
            this.head = node; 
        }else{
            let origin = this.getElementAt(this.length - 1);
            origin.next = node;
            node.prior = origin;
        }
        this.tail = node;
        this.length++;
    }
    // 在某位置插入1个结点
    insert(position, element) { 
        let node = new DoubleNode(element);
        if(position < 0 || position > this.length){
            return;
        }else{
            if(this.head === null){
                this.head = node;
                this.tail = node;
            }else{
                // 插在头结点
                if(position === 0){ 
                    node.next = this.head;
                    this.head.prior = node;
                    this.head = node;
                }else{
                    let origin = this.getElementAt(position - 1);
                    node.prior = origin;
                    if(origin.next){
                        // 插在中间
                        node.next = origin.next; 
                        origin.next.prior = node;
                        origin.next = node;
                    }else{
                        // 插在最后一位 
                        node.next = null;
                        origin.next = node;  
                        this.tail = node;
                    }
                    
                }

            }
            this.length++;
        } 
    }
    // 在某位置删除1个结点
    delete(position) {
        if(position < 0 || position > this.length){
            return;
        }else{
            if(position === 0){  
                this.head = this.head.next;
                this.head.prior = null;
            }else{ 
                let current = this.getElementAt(position);
                // 删除中间位
                if(current.next){ 
                    current.prior.next = current.next;
                    current.next.prior = current.prior;
                }else{
                    // 删除最后一位  
                    current.prior.next = null; 
                    this.tail = current.prior;
                } 
            } 
            this.length--;
        }

    }
}

 

posted @ 2020-10-29 19:55  Isabel丶  阅读(157)  评论(0编辑  收藏  举报