class Node{ constructor(element){ this.element=element; this.pre=null; this.next=null; } } class DoubleLink{ constructor(){ this.head=null; this.tail=null; this.length=0; } Append(element) { var node=new Node(element); if(this.head==null) { this.head=node; this.tail=node; } else{ this.tail.next=node; node.pre=this.tail; this.tail=node; } this.length++; } Insert(element,position){ if(position<0 || position>this.length) return false; var node=new Node(element); //在头结点插入 if(position==0) { if(this.head==null) { this.head=node; this.tail=node; } else{ node.next=this.head; this.head.pre=node; this.head=node; } } //在中间节点插入 if(position>0 && position<this.length){ var index=0; var current=this.head; var pre=this.head; while(index++<position){ pre=current; current=current.next; } node.next=current; node.pre=current.pre; current.pre=node; pre.next=node; } //在尾节点插入 if(position==this.length && position>0){ this.tail.next=node; node.pre=this.tail; this.tail=node; } this.length++; return true; } RemoveAt(position){ if(position<0 || position>=this.length) return false; //头 if(position==0){ this.head=this.head.next; this.head.pre=null; if(this.length==1){ this.tail=null; } } //尾 if(position==this.length-1 && position>0){ this.tail=this.tail.pre; this.tail.next=null; } //中间 if(position>0 && position<this.length-1){ var index=0; var current=this.head; var previous=this.head; while(index++<position){ previous=current; current=current.next; } previous.next=current.next; current.next.pre=previous; } this.length--; return true; } } var double=new DoubleLink(); double.Append(1);