DoublyLinkedList

//双向链表
            //链接是双向的,一个链向下一个元素,一个链向上一个元素,同时控制next和prev两个指针
            //head/tail
            function DoublyLinkedList(){
                function Node(data){
                    this.data = null;
                    this.prev = null;
                    this.next = null;
                }
                this.head = null;
                this.tail = null;
                this.length = 0;
                DoublyLinkedList.prototype.append = function (data){
                    var newNode = new Node(data);
                    if(this.length == 0){
                        this.head = newNode;
                        this.tail = newNode;
                    }else{
                        newNode.prev = this.tail;
                        this.tail.next = newNode;
                        this.tail = newNode;
                    }
                    this.length += 1;
                }
                DoublyLinkedList.prototype.toString = function(){
                    return this.backwardString();
                }
                DoublyLinkedList.prototype.forwardString = function(){
                    var current = this.tail;
                    var resultString = "";
                    while(current){
                        resultString += current.data + "";
                        current = current.prev;
                    }
                    return resultString;
                }

                DoublyLinkedList.prototype.backwardString = function(){
                    var current = this.head;
                    var resultString = "";
                    while(current){
                        resultString += current.data + "";
                        current = current.next;
                    }
                    return resultString;
                }
                
                DoublyLinkedList.prototype.insert = function(position, data){
                    if(position < 0 || position > this.length) return false;
                    var newNode = new Node(data);
                    if(this.length == 0){
                        this.head = newNode;
                        this.tail = newNode;
                    }else{
                        if(postion == 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{
                            var current = this.head;
                            var index = 0;
                            while(index++ < position){
                                current = current.next;
                            }
                            newNode.next = current;
                            newNode.prev = current.prev;
                            current.prev.next = newNode;
                            current.prev = newNode;
                        }
                    }
                    this.length += 1;
                    return true;
                }
                DoublyLinkedList.prototype.get = function(position){
                    if(position < 0 || position >= this.length) return null;
                    var current = this.head;
                    var index = 0;
                    while(index++ < position){
                        current = current.next;
                    }
                    return current.data;
                }
                DoublyLinkedList.prototype.indexOf = function(data){
                    var current = this.head;
                    var index = 0;
                    while(current){
                        if(current.data == data){
                            return index;
                        }
                        current = current.next;
                        index += 1;
                    }
                    return -1;
                }
                DoublyLinkedList.prototype.update = function(position, newData){
                    if(position < 0 || position >= this.length) return false;
                    var current = this.head;
                    var index = 0;
                    while(index++ < position){
                        current = current.next;
                    }
                    current.data = newData;
                    return true;
                }
                DoublyLinkedList.prototype.removeAt = function(position){
                    if(position < 0 || position >= this.length) return null;
                    var 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){
                            var current = this.tail;
                            this.tail.prev.next = null;
                            this.tail = this.tail.prev;
                        }else{
                            var index = 0;
                            while(index++ < position){
                                current = current.next;
                            }
                            current.prev.next = current.next;
                            current.next.prev = current.prev;
                        }
                    }
                    this.length -=1;
                    return current.data;
                }
                DoublyLinkedList.prototype.remove = function(){
                    var index = this.indexOf(data);
                    return this.removeAt(index);
                }
            }

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值