/** * Implementation moves n nodes forward in the list and moves backwards n nodes * **///Linked list NodesfunctionNode (Element) { This. Element =element; This. Next =NULL; This. Previous =NULL;}//Linked listfunctionllist () { This. Head =NewNode (' head '); This. Find =find; This. Insert =Insert; This. display =display; This. remove =remove; This. FindLast =FindLast; This. Dispreverse =Dispreverse; //The current node is the head node This. CurrentNode = This. Head; //Move n nodes forward from the beginning of the list This. Advance =Advance; //Roll back N nodes from a list of nodes This. Back =Back ; //Show current Node This. Show =Show;}//all nodes in the reverse output listfunctionDispreverse () {varCurrnode = This. Head; Currnode= This. FindLast (); while(! (Currnode.previous = =NULL) {document.write (currnode.element+ ' '); Currnode=currnode.previous; }}//find the last nodefunctionFindLast () {varCurrnode = This. Head; while(! (Currnode.next = =NULL) ) {Currnode=Currnode.next; } returnCurrnode;}//Delete a nodefunctionRemove (item) {varCurrnode = This. Find (item); if(! (Currnode.next = =NULL) ) {CurrNode.previous.next=Currnode.next; CurrNode.next.previous=currnode.previous; Currnode.next=NULL; Currnode.previous=NULL; }}//Print all linked list nodesfunctiondisplay () {varCurrnode = This. Head; while(! (Currnode.next = =NULL) {document.write (currNode.next.element+ ' '); Currnode=Currnode.next; }}//Find a nodefunctionFind (item) {varCurrnode = This. Head; while(Currnode.element! =Item) {Currnode=Currnode.next; } returnCurrnode;}//Insert a nodefunctionInsert (newelement, item) {varNewNode =NewNode (newelement); varCurrent = This. Find (item); Newnode.next=Current.next; Newnode.previous=Current ; Current.next=NewNode;}//Move n nodes forward in a linked listfunctionadvance (n) { while((n>0) &&! ( This. currentnode.next==NULL)){ This. CurrentNode = This. Currentnode.next; N-- }}//Moving backward n nodes in a linked listfunctionBack (n) { while(n>0 &&!) ( This. currentnode.element== ' head ')){ This. CurrentNode = This. currentnode.previous; N--; }}//Show current NodefunctionShow () {document.write ( This. currentnode.element);}varCities =Newllist (); Cities.insert (' Conway ', ' head '); Cities.insert (' Russellville ', ' Conway '); Cities.insert (' Carlisle ', ' Russellville '); Cities.insert (' Alma ', ' Carlisle '); Cities.insert (' Dezhou ', ' Alma '); Cities.insert (' Alasijia ', ' Dezhou '); Cities.display ();d Ocument.write (' <br> '); Cities.show (); Cities.advance (4);d Ocument.write (' <br> '); Cities.show (); Cities.back (2);d Ocument.write (' <br> '); Cities.show ();
Move n nodes forward (backward) in a linked list in JavaScript