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