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);
                }
            }
发布了34 篇原创文章 · 获赞 34 · 访问量 1086

猜你喜欢

转载自blog.csdn.net/qq_45517916/article/details/104611529
今日推荐