JS to implement a doubly linked list, doubly linked list needs to add a previous property

Source: Internet
Author: User

Doubly linked list,
Doubly linked list needs to add a previous property
/*doubly linked list **/functionNode (Element) { This. Element =element;  This. Next =NULL;  This. Previous =NULL;//the doubly linked list here needs to add a previous property}functionllist () { This. Head =NewNode ("Head");  This. Find =find;  This. Insert =Insert;  This. display =display;  This. remove =remove;  This. FindLast =FindLast;  This. Dispreverse = Dispreverse;//invert a linked list}functionDispreverse () {varCurrnode = This. Head; Currnode= This. FindLast (); varNodestr = "";  while(! (Currnode.previous = =NULL) ) {Nodestr+= " "+currnode.element; Currnode=currnode.previous; } console.log ("Invert the list after:" +nodestr);}functionFindLast () {varCurrnode = This. Head;  while(! (Currnode.next = =NULL) ) {Currnode=Currnode.next; }    returnCurrnode;}functionRemove (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; }}//findprevious is no longer needed/*function FindPrevious (item) {var currnode = This.head; Currnode.next = = null) && (currNode.next.element = Item) {Currnode = Currnode.next;} return currnode; }*/functiondisplay () {varCurrnode = This. Head; varNodestr = "";  while(! (Currnode.next = =NULL) ) {Nodestr+= " "+currNode.next.element; Currnode=Currnode.next; } console.log (NODESTR);}functionFind (item) {varCurrnode = This. Head;  while(Currnode.element! =Item) {Currnode=Currnode.next; }    returnCurrnode;}functionInsert (newelement, item) {varNewNode =NewNode (newelement); varCurrent = This. Find (item); Newnode.next=Current.next; Newnode.previous= current;//the doubly linked list here needs to set the new node previous propertyCurrent.next =NewNode;}varCities =Newllist (); Cities.insert ("Conway", "head"); Cities.insert ("Russellville", "Conway"); Cities.insert ("Carlisle", "Russellville"); Cities.insert ("Alma", "Carlisle"); Cities.display ();//Conway Russellville Carlisle AlmaCities.remove ("Carlisle"); Cities.display ();//Conway Russellville AlmaCities.dispreverse ();//Alma Russellville Conway

JS to implement a doubly linked list, doubly linked list needs to add a previous property

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.