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