Java implementation of doubly linked list

Source: Internet
Author: User

1  Packagestructure;2 3 Importjava.util.Arrays;4 ImportJava.util.Scanner;5 Import Staticnet.mindview.util.print.*;6 7 /**8 * operation of doubly linked list9  * @authorTom TaoTen  * One  */ A classNode { -      Public intvalue; -      PublicNode (intN) { the          This. Value =N; -     } - Node pre; - Node Next; + } -  +  Public classRelinkedlist { A      Public StaticNode head; at      Public Static intNodenum; -      -     /*Initialize*/ -      Public Static voidInitintn[]) { -Head =NewNode (n[0]); -Node temp =NULL; inHead.pre =NULL; -Node p =head; toNodenum + +; +          for(inti = 1; N[i]! =-1; i + +) { -Nodenum + +; thetemp =NewNode (N[i]); *P.next =temp; $Temp.pre =p;Panax Notoginsengp =temp; -         } theP.next =NULL; +     } A      the     /*Print*/ +      Public Static voidprintlist (Node h) { -          while(H! =NULL) { $PRINTNB (H.value + ""); $h =H.next; -         } -     } the      -     /*Insert*/Wuyi      Public Static voidInsertintIndexintv) { theNode NE =NewNode (v); -         if(Index = = 0) { WuNe.next =head; -Ne.pre =NULL; AboutHead =NE; $             return; -         } -Node temp =head; -Node T =temp; A          for(inti = 0; I < index; i + +) { +t =temp; thetemp =Temp.next; -         } $         if(Index = =nodenum) { theT.next =NE; theNe.pre =T; theNe.next =NULL; the             return; -         } inNe.next =temp; theNe.pre =T; theTemp.pre =NE; AboutT.next =NE; the     } the      the     /*Delete*/ +      Public Static voidDeleteintindex) { -         if(Index = = 0){ theHead =Head.next;BayiHead.pre =NULL; the             return; the         } -Node temp =head; -Node T =temp; the          for(inti = 0; I < index; i + +) { thet =temp; thetemp =Temp.next; the         } -         if(Index = =nodenum) { theT.next =NULL; the             return; the         }94T.next =Temp.next; theTemp.next.pre =T; the     } the     98     /*Find*/ About      Public Static intFindintindex) { -Node temp =head;101          for(inti = 0; I < index; i + +) {102temp =Temp.next;103         }104         returnTemp.value; the     }106     107     /*Test*/108      Public Static voidMain (string[] args) {109Scanner sc =NewScanner (system.in); thePrint ("Please input the nums to init:");111         int[] n =New int[100]; theArrays.fill (n,-1);113         intVal; the         intCount = 0; the          while(val = Sc.nextint ()) >= 0) { then[count++] =Val;117         }118 init (n);119          while(true){ - Printlist (head);121Print ("\nplease input the index and NUM to insert");122             intindex =sc.nextint ();123val =sc.nextint ();124 Insert (index, Val); the Printlist (head);126Print ("\nplease input the index to delete");127index =sc.nextint (); - Delete (index);129 Printlist (head); thePrint ("\nplease input the index to find");131index =sc.nextint (); the print (find (index));133Print ("----------------------------------------------------");134         }135     }136}

Java implementation of doubly linked list

Related Article

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.