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--;
            }
    
        }
    }
  • 相关阅读:
    201-STM32+Air724UG基本控制篇(阿里云物联网平台)-设备使用物模型Topic上报温湿度数据
    Sentinel Go 核心统计结构滑动窗口的深度解析
    golang sync.Mutex互斥锁的实现原理
    Golang-Scheduler原理解析
    Golang-Channel原理解析
    golang里channel的实现原理
    最长回文子序列
    GO语言的goroutine并发原理和调度机制
    golang 常见问题
    通过js给网页加上水印背景
  • 原文地址:https://www.cnblogs.com/Isabel-1996/p/13898873.html
Copyright © 2011-2022 走看看