JS中的双向链表

1 双向链表的特点

  1. 可以使用一个head和一个tail分别指向头部和尾部节点
  2. 每个节点都由三部分组成:前一个节点的指针(prev)/ 保存的元素(item)/ 后一个节点的指针(next)
  3. 双向链表的第一个节点的prev是null
  4. 双向链表的最后一个节点的next是null
    在这里插入图片描述

2 双向链表的封装

function DoublyLinkList(){
            //内部类:节点类
            function Node(data){
                this.data = data;
                this.prev = null;
                this.next = null;
            }

            //属性
            this.head = null;
            this.tail = null;
            this.length = 0;

            //常见的操作:方法
            //1.append方法
            DoublyLinkList.prototype.append = function(data){
                //1.根据data创建节点
                var newNode = new Node(data);
                //2.判断添加的是否是第一个节点
                if(this.length == 0){
                    this.head = newNode;
                    this.tail = newNode;
                }else{
                    newNode.prev = this.tail;
                    this.tail.next = newNode;
                    this.tail = newNode;
                }
                //3、length+1
                this.length ++;
            }
            //2.将链表转成字符串形式
            //2.1 toString 方法
            DoublyLinkList.prototype.toString = function(){
                return this.backwardString();
            }
            //2.2 forwardString 方法
            DoublyLinkList.prototype.forwardString = function(){
               //1. 定义变量
                var current = this.tail;
                var resultString = "";

                //2.依次向后遍历,获取每一个节点
                while(current){
                    resultString += current.data + ""; 
                    current = current.prev;
                }
                return resultString;
            }
            //2.3 backwardString 方法
            DoublyLinkList.prototype.backwardString = function(){
                //1. 定义变量
                var current = this.head;
                var resultString = "";

                //2.依次向后遍历,获取每一个节点
                while(current){
                    resultString += current.data + ""; 
                    current = current.next
                }
                return resultString;
            }
            //3、insert方法
            DoublyLinkList.prototype.insert = function(position,data){
                //1.越界判断
                if(position<0 || position>this.length){
                    return false
                }else{
                    //2.根据data 创建新的节点
                    var newNode = new Node(data);
                    //3.判断原来的列表是否为空
                    if(this.length == 0){
                        this.head = newNode;
                        this.tail = newNode;
                    }else{
                        //3.1判断position是否为0
                        if(position === 0){
                            this.head.prev = newNode;
                            newNode.next = this.head;
                            this.head = newNode;
                        }else if(position == this.length){
                         //3.2 position为最后一个节点
                         newNode.prev = this.tail;
                         this.tail.next = newNode;
                         this.tail = newNode;
                        }else{
                         //3.3 psition是中间的某个位置
                         var current = this.head;
                         var index = 0;
                         while(index<position){
                             current = current.next;
                             index++;
                         }
                         //修改指针
                         current.prev.next = newNode;
                         newNode.prev = current.prev;
                         newNode.next = current;
                         current.prev = newNode;
                        }
                        //length+1
                        this.length++;
                    }
                    return true
                }
            }
            //4、get方法
            DoublyLinkList.prototype.get = function(position){
                //1.越界判断
                if(position<0 || position >= this.length){
                    return null;
                }else{
                    //2.获取元素
                    var current = this.head;
                    var index = 0;

                    while(index < position){
                        current = current.next;
                        index++;
                    }
                    return current.data;
                }
            }
            //5、indexOf方法
            DoublyLinkList.prototype.indexOf = function(data){
                //1.定义变量
                var current = this.head;
                var index = 0;
                //2.查找到和data相同的节点
                while(current){
                    if(current.data == data){
                        return index;
                    }else{
                        current = current.next;
                        index++;
                    }
                }
                return -1;
            }
        
            //6、update方法
            DoublyLinkList.prototype.update = function(position,newData){
                //1.越界判断
                if(position < 0 || position >= this.length){
                    return false
                }
                //2.寻找正确的节点
                var current = this.head;
                var index = 0;
                while(index < position){
                    current = current.next;
                    index++;
                }
                //3.修改找到节点的data信息
                current.data = newData;
                return newData;
            }
            //7、removeAt方法
            DoublyLinkList.prototype.removeAt = function(position){
                //1.越界判断
                if(position < 0 || position >= this.length){
                    return null;
                }else{
                    //2.1 判断是否只有一个节点
                    var current = this.head;
                    if(this.length === 1){
                        this.head = null;
                        this.tail = null;
                    }else{
                        //2.2.判断是否删除第一个节点
                        if(position === 0){
                            this.head.next.prev = null;
                            this.head = this.head.next;
                            //2.3 判断是否删除最后一个节点
                        }else if(position === this.length - 1){
                            current = this.tail
                            this.tail.prev.next = null;
                            this.tail = this.tail.prev;
                            //2.4 判断是否删除中间节点
                        }else{
                            var index = 0;
                            // var current = this.head;
                            while(index < position){
                                current = current.next;
                                index++;
                            }
                            current.prev.next = current.next;
                            current.next.prev = current.prev;
                        }
                    }
                    this.length--;
                    return current.data;
                }
            }
            //8.remove方法
            DoublyLinkList.prototype.remove = function(data){
                //1.根据data获取下标值
                var index = this.indexOf(data);
                //2.根据Index删除对应位置的节点
                return this.removeAt(index);
            }
            //9、isEmpty 方法
            DoublyLinkList.prototype.isEmpty = function(){
                return this.length === 0;
            }
            //10、size方法
            DoublyLinkList.prototype.size = function(){
                return this.length;
            }
            //11、获取链表的第一个元素
            DoublyLinkList.prototype.getHead = function(){
                return this.head;
            }
             //12、获取链表的最后一个元素
             DoublyLinkList.prototype.getTail = function(){
                return this.tail;
            }
        }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值