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);
                }
            }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章