Bidirectional linked list--java implementation

Source: Internet
Author: User

1 /*two-way linked list features:2 * * Each node contains two references, Previos and next, supporting forward or backward traversal (except the head node)3 when inserting or deleting a defect, it involves more than a reference modification .4 * Note: The following doubly linked list actually implements the double-ended linked list5 * Note: Multiple references in Java can point to the same object, or you can change the reference point at any time6 * To modify the reference carefully, you can refer to a = reference B means a reference to the object that the B reference points to7 * Application: Double-ended queue can be realized by using doubly linked list8  * */9 Ten  Public classMydoublelink { One     PrivateLink first; A     PrivateLink last; -      -      Public BooleanIsEmpty () { the         returnFirst = =NULL; -     } -      -      Public voidInsertfirst (intkey) { +Link NewLink =NewLink (key); -         if(First = =NULL){ +Last =NewLink; A         } at         Else{ -First.previous =NewLink; -         } -Newlink.next = First;//The chain is not broken can point to the same -First =NewLink; -     } in      -      Public voidInsertlast (intkey) { toLink NewLink =NewLink (key); +         if(First = =NULL){ -First =NewLink; the         } *         Else{ $Last.next =NewLink;Panax NotoginsengNewlink.previous =Last ; -         } theLast =NewLink; +     } A      the     //Insert the following---the specified value is actually a tail insert-the list is not empty to operate +      Public BooleanInsertAfter (intKeyintvalue) { -Link NewLink =NewLink (value); $Link current =First ; $          while(Current.id! =key) { -Current =Current.next; -             if(Current = =NULL){ the                 return false; -             }Wuyi         } the         if(current = = last) {//find it at last item -Newlink.next =NULL; WuLast =NewLink; -         } About         Else{ $Newlink.next =Current.next; -Current.next.previous =NewLink; -         } -Newlink.previous =Current ; ACurrent.next =NewLink; +         return true; the     } -      $      PublicLink Deletefirst () { theLink temp =First ; the         if(First.next = =NULL){ theLast =NULL; the         } -         Else{ inFirst.next.previous =NULL; the         } theFirst =First.next; About         returntemp; the     } the      the      PublicLink Deletelast () { +Link temp =Last ; -         if(First.next = =NULL){ theFirst =NULL;Bayi         } the         Else{ theLast.previous.next =NULL; -         } -Last =last.previous; the         returntemp; the     } the      the     //Delete by value--there may be no time to find it -      PublicLink Delete (intkey) { theLink current =First ; the          while(Current.id! =key) { theCurrent =Current.next;94             if(Current = =NULL){ the                 return NULL; the             } the         }98         if(current = = first) {//find it at first item is not just one node AboutFirst =Current.next; -         }101         Else{//find it not first item102Current.previous.next =Current.next;103         }104          the         if(current = = last) {//find it at last item106Last =current.previous;107         }108         Else{//find it not last109Current.next.previous =current.previous; the         }111         returnCurrent ; the     }113      the      Public voidDiaplayfirsttolast () { theSystem.out.println ("First to Last"); theLink current =First ;117          while(Current! =NULL){118System.out.print (current.id + "");119Current =Current.next; -         }121 System.out.println ();122     }123     124      Public voidDisplaylasttofirst () { theSystem.out.println ("Last to First");126Link current =Last ;127          while(Current! =NULL){ -System.out.print (current.id + "");129Current =current.previous; the         }131 System.out.println (); the     }133}

Bidirectional linked list--java implementation

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.