A simple implementation of the chain list
1 Packagetest01;2 3 /*4 * Simple implementation of one-way linked list5 * */6 7 classnode{8 PrivateString data;9 PrivateNode Next;Ten PublicNode (String data) { One This. data =data; A } - PublicString GetData () { - returndata; the } - Public voidsetData (String data) { - This. data =data; - } + PublicNode GetNext () { - returnNext; + } A Public voidSetnext (Node next) { at This. Next =Next; - } - } - - Public classLianbiao { - in Public Static voidMain (string[] args) { -Node N0 =NewNode ("A"); toNode N1 =NewNode ("B"); +Node N2 =NewNode ("C"); - N0.setnext (N1); the N1.setnext (n2); * Print (n0); $ }Panax Notoginseng - Private Static voidPrint (Node n0) { the System.out.println (N0.getdata ()); + if(N0.getnext ()! =NULL){ A Print (N0.getnext ()); the } + - } $ $}
Second, the authentic realization of the list
1 Packagetest02;2 3 /*4 * One-way linked list of authentic implementation5 * */6 7 classlink{8 classnode{9 PrivateString data;Ten PrivateNode Next; One PublicNode (String data) { A This. data =data; - } - Public voidAddNode (Node newNode) { the if( This. Next = =NULL){ - This. Next =NewNode; -}Else{ - This. Next.addnode (NewNode); + } - } + Public voidPrintnode () { ASystem.out.println ( This. data); at if( This. Next! =NULL){ - This. Next.printnode (); - } - } - } - Node Root; in Public voidAdd (String data) { -Node NewNode =NewNode (data);//The first step is to build the node, then you can refer to the simple implementation of the linked list to if( This. root = =NULL){ + This. root =NewNode; -}Else{ the This. Root.addnode (NewNode); * } $ }Panax Notoginseng Public voidPrintnode () { - This. Root.printnode (); the } + } A the Public classLianBiao01 { + - Public Static voidMain (string[] args) { $Link L =NewLink (); $L.add ("ROOT"); -L.add ("A"); -L.add ("B"); theL.add ("C"); - L.printnode ();Wuyi } the}
Java implementation of one-way linked list