The realization of double-end linked list--java

Source: Internet
Author: User

1 /*double-ended linked list--a reference to the last node more than the regular list2 * Features: Linked list can be used for tail insertion-the output sequence is consistent with the input sequence3 * But no tail deletion is possible because there is no reference to the penultimate node4  * */5  Public classMyfirstandlastlinkedlist {6      PublicLink first;7      PublicLink last;8     9      Publicmyfirstandlastlinkedlist () {TenFirst =NULL; OneLast =NULL; A     } -      -      Public BooleanIsEmpty () { the         returnFirst = =NULL; -     } -      -     //when the head is inserted, pay attention to the last processing of the empty list +      Public voidInsertfirst (intkey) { -Link NewLink =NewLink (key); +         if(IsEmpty ()) { ALast =NewLink; at         } -Newlink.next =First ; -First =NewLink; -     } -      -     //when the tail is inserted, pay attention to the empty list handling of first in      Public voidInsertlast (intkey) { -Link NewLink =NewLink (key); to         if(IsEmpty ()) { +First =NewLink; -         } the         Else{ *Last.next =NewLink; $         }Panax NotoginsengLast =NewLink; -     } the      +     //Delete Note Only one node handles the last A      PublicLink Deletefirst () { theLink temp =First ; +         if(First.next = =NULL){ -Last =NULL; $         } $First =First.next; -         returntemp; -     } the      -      Public voidDisplaylinkedlist () {//Straight chain PrintingWuyi         //System.out.println ("First---to----last"); theLink current =First ; -          while(current!=NULL ){ Wu Current.diaplaylink (); -System.out.print (""); AboutCurrent =Current.next; $         } - System.out.println (); -     } -      A     //Test the class +      Public Static voidMain (string[] args) { the         int[] arr = {1,2,3,4,5,6}; -Myfirstandlastlinkedlist m =Newmyfirstandlastlinkedlist (); $          the          for(inti = 0; i < arr.length; i++){ the M.insertlast (Arr[i]); the         } the m.displaylinkedlist (); -          in          the     } the      About}

The realization of double-end linked list--java

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.