Linked list: is a common basic data structure, is a linear table, but does not store data in a linear order, but in each node in the village to the next node pointer.
In a linked list data structure, a recursive algorithm is required. Recursive algorithm is a process of invoking its own algorithm directly or indirectly.
1 Public classPractice14 {2 3 Public Static voidMain (string[] args) {4 //TODO auto-generated Method Stub5Jiecheng (5);6System.out.println (Jiecheng2 (5));7 }8 //recursive notation (very simple)9 //Note that recursion must have an exit, too many recursion can cause a memory stack overflowTen Public Static intJiecheng2 (intnum) { One if(num==1)return1; A returnNum*jiecheng2 (num-1); - } - //find factorial (common notation) the Public Static voidJiecheng (intnum) { - - intsum=num; - intI=num-1; + Do{ -sum=sum*i; +i--; A} while(i>1); at System.out.println (sum); - } - -}
The linked list data structure is used for frequent add, insert, delete operations, for example:
1 //linked list data structure for frequent add, insert, delete operations2 Public classPractice14 {3 4 Public Static voidMain (string[] args) {5 //TODO auto-generated Method Stub6NodeManager nm=NewNodeManager ();7Nm.addnode ("1");8Nm.addnode ("2");9Nm.addnode ("3");TenNm.addnode ("4"); One Nm.printnode (); ANm.delnode ("3"); - Nm.printnode (); - } the - } - //Node Management classes - classnodemanager{ + PrivateNode Root;//root node - Public voidAddNode (String name) { + if(root==NULL){ Aroot=NewNode (name); at}Else{ - Root.add (name); - } - } - - Public voidDelnode (String name) { in if(Root.getname (). Equals (name)) { -root=Root.next; to}Else{ + Root.del (name); - } the * } $ Panax Notoginseng Public voidPrintnode () { - if(root!=NULL){ theSystem.out.print (Root.getname () + "-"); + root.print (); A System.out.println (); the } + - } $ //each Node object $ classnode{ - PrivateString name; - PrivateNode Next;//represents the next node of the current node the PublicNode (String name) { - This. name=name;Wuyi } the - PublicString GetName () { Wu returnname; - } About Public voidsetName (String name) { $ This. Name =name; - } - PublicNode GetNext () { - returnNext; A } + Public voidSetnext (Node next) { the This. Next =Next; - } $ //Adding nodes the Public voidAdd (String name) { the if( This. next==NULL){ the This. next=NewNode (name); the}Else{ - This. Next.add (name);//Recursive in } the } the //Delete a node About Public voiddel (String name) { the if( This. next!=NULL){ the if( This. Next.name.equals (name)) { the This. next= This. Next.next; +}Else{ - This. Next.del (name); the }Bayi } the } the //Output Node - Public voidprint () { - if( This. next!=NULL){ theSystem.out.print ( This. Next.getname () + "-"); the This. Next.print (); the } the } - } the}
A linked list of Java object-oriented data structures