//get方法
DoubleLinkedList.prototype.get=function(position){//1.越界判断if(position <0|| position >=this.length){returnnull;}//如果this.length / 2 > position 从前往后遍历//如果this.length / 2 < position 从后往前遍历var previous =this.tail;var current =this.head;if(position <this.length /2){var index =0;while(index++< position){
current = current.next;}return current.data
}else{var index =this.length -1;while(index--> position){
previous = previous.prev;}return previous.data;}}
6.5updata方法
//updata()方法
DoubleLinkedList.prototype.updata=function(position, newData){//边界判断if(position <0|| position >this.length){returnfalse;}//寻找if(position <this.length /2){var index =0;var current =this.head;while(index++< position){
current = current.next;}
current.data = newData;}else{var index =this.length -1;var previous =this.tail;while(position < index--){
previous = previous.prev;}
previous.data = newData;}}
6.6indexOf方法
//indexOf()方法
DoubleLinkedList.prototype.indexOf=function(element){var index =0;var current =this.head;while(current){if(current.data == element){return index;}
current = current.next;
index++;}return-1;}
6.7removeAt方法
//removeAt方法
DoubleLinkedList.prototype.removeAt=function(position){//1.边界判断if(position <0|| position >this.length){returnfalse;}//2.加入只有一个节点(长度唯一)if(this.length ==1){this.head =null;this.tail =null;//长度不为1的时候判断删除的是否是第一个节点}else{if(position ==0){this.head.next.prev =null;this.head =this.head.next;}elseif(position ==this.length -1){this.tail.prev.next =null;this.tail =this.tail.prev;}else{var index =0;if(position <this.length /2){var current =this.head;while(index++< position){
current = current.next;}
current.prev.next = current.next;
current.next.prev = current.prev;}else{var previous =this.tail;var index =this.length -1;while(position < index--){
previous = previous.prev;}
previous.prev.next = previous.next;
previous.next.prev = previous.prev;}}}this.length -=1;returntrue;}
6.8remove方法
//8.remove方法
LinkedList.prototype.remove=function(data){//1.获取data在列表中的位置let position =this.indexOf(data);//2.根据位置信息,删除节点//3.length - 1this.length -=1;returnthis.removeAt(position);}