Move n nodes forward (backward) in a linked list in JavaScript

Source: Internet
Author: User

/** * 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+ ' &nbsp; '); 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+ ' &nbsp; '); 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.