zoukankan      html  css  js  c++  java
  • 双向链表实现查询、删除、插入、末尾增加

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

    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--;
            }
    
        }
    }
  • 相关阅读:
    学习曲线
    正则化——“偏差(bias)”与“方差(variance)”
    诊断偏差(bias)和方差(variance)
    模型选择和训练/验证/测试数据集
    运用机器学习的建议
    训练神经网络的一般步骤
    Java数组
    类型信息(反射,RTTI)
    equals(), "== ",hashcode() 详细解释
    java并发
  • 原文地址:https://www.cnblogs.com/Isabel-1996/p/13898873.html
Copyright © 2011-2022 走看看