function LinkedList () {//helper class, which represents each of the added list's var node=function (element) {this.element=element; This.next=null; }//list, the length of the storage chain is Var length=0; var head=null; This.append=function (Element) {var node=new node (element), current; if (head===null) {head=node; }else{Current=head; Find the last element in the list while (Current.next) {current=current.next; } Current.next=node; } length++; }; This.insert=function (pos,element) {if (pos>=0 && pos<=length) {var node=new node (elemen t), current=head,previous,index=0; if (position===0) {node.next=current; Head=node; }else{while (index++<pos) {previous=current; Current=current.next; } node.next=current; prEvious.next=node; } length++; return true; }else{return false; } }; This.removeat=function (POS) {if (pos>-1 && pos<length) {var current=head,previous,index=0 ; if (pos==0) {head=current.next; }else{while (index++<pos) {previous=current; Current=current.next; } Previous.next=current.next; } length--; return current.element; }else{return null; } }; This.remove=function (Element) {var index=this.indexof (element); Return This.removeat (index);//Not very good, repeated traversal 2 times}; This.indexof=function (Element) {var current=head,index=0; while (current) {if (element===current.element) {return index; } index++; Current=current.next; } return-1; }; This.isempty=function () {return length===0; }; This.size=function () {return length; }; This.tostring=function () {var current=head,str= '; while (current) {str+= "," +current.element; Current=current.next; } return Str.slice (1); }; This.print=function () {Console.log (this.tostring ()); }; This.gethead=function () {return head; }} function Doublylinkedlist () {//helper class, representing each item of Var node=function (element) {This.elem added to the list Ent=element; This.next=null; This.prev=null; }//list, the length of the storage chain is Var length=0; var head=null; var tail=null; This.append=function (Element) {var node=new node (element), current; if (head===null) {head=node; }else{Current=head; Find the last element in the list while (Current.next) {current=current.next; } Current.next=node; } length++; }; This.insert=function (pos,element) {if (pos>=0 && pos<=length) {var node=new node (elemen t), current=head,previous,index=0; if (pos===0) {if (!head) {head=node; Tail=node; }else{node.next=current; Current.prev=node; Head=node; }}else if (pos==length) {current=tail; Current.next=node; Node.prev=current; Tail=node; }else{while (index++<pos) {previous=current; Current=current.next; } node.next=current; node.prev=previous; Previous.next=node; Current.prev=node; } length++; return true; }else{return false; } }; This.removeat=function (POS) {if (pos>-1 && pos<length) {var current=head,previous,index=0 ; if (pos==0) {head=current.next; if (!length===1) {tail=null; }else{Head.prev=null; }}else if (pos==length-1) {current=tail; Tail=current.prev; Tail.next=null; }else{while (index++<pos) {previous=current; Current=current.next; } Previous.next=current.next; Current.next.prev=current.prev; } length--; return current.element; }else{return null; } }; This.remove=function (Element) {var index=this.indexof (element); return this. removeAt (index);//Not very good, repeated traversal 2 times}; This.indexof=function (Element) {var current=head,index=0; while (current) {if (element===current.element) {return index; } index++; Current=current.next; } return-1; }; This.isempty=function () {return length===0; }; This.size=function () {return length; }; This.tostring=function () {var current=head,str= '; while (current) {str+= "," +current.element; Current=current.next; } return Str.slice (1); }; This.print=function () {Console.log (this.tostring ()); }; This.gethead=function () {return head; }}
List of links in JavaScript