Java implementation of one-way linked list

Source: Internet
Author: User

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

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.