Single-linked list Java implementation

Source: Internet
Author: User

Single-linked list Java implementation, the basic things can not forget oh. These are all regular exams at college.

 Packagedatastructure;ImportJava.util.HashMap;/** * @authorJesh * *@param<T>*/ Public classLinklist<t> {        Private  FinalNode<t>Head;  Publiclinklist () {head=NewNode<t> (NULL); }         Public voidAddNode (T data) {Node<T> n =NewNode<t>(data); Node<T> tmp =Head;  while(Tmp.getnext ()! =NULL) {tmp=Tmp.getnext ();    } tmp.setnext (n); }         PublicT GetData (intindex) {        if(Index <0)        {            return NULL; }        inti = 0; Node<T> tmp =Head;  while(Tmp.getnext ()! =NULL)        {            if(i++ = =index) {                return(T) tmp.data; } tmp=Tmp.getnext (); }        if(i = =index) {            return(T) tmp.data; }        return NULL; }             Public BooleanDelnode (T data) {if(Data = =NULL)            return false; Node tmp=Head;  while(Tmp.getnext ()! =NULL)        {            if(Tmp.getnext (). Data.equals (data)) {Tmp.setnext (Tmp.getnext (). GetNext ()); return true; } tmp=Tmp.getnext (); }        return false; }         PublicString toString () {StringBuffer sb=NewStringBuffer (); Node T=Head; Sb.append ("Head:" +t.data);  while(T.getnext ()! =NULL) {T=T.getnext (); Sb.append ("--"). Append ("node:" +t.data.tostring ()); } sb.append ("-->null"); returnsb.tostring (); }            Private classNode<t>{T data; PrivateNode next =NULL;  PublicNode (T data) { This. data =data; }         PublicNode GetNext () {returnNext; }         Public voidSetnext (Node next) { This. Next =Next; }    }     Public Static voidMain (string[] args) {linklist<Integer> LL =NewLinklist<integer>();  for(inti = 0;i<100;i++) {Ll.addnode (i); }         for(intJ =0;j<108;j++) {System.out.println (Ll.getdata (j)); }         for(intJ =0;j<108;j++)        {            if(j%3==0) {Ll.delnode (j);                }} System.out.println (LL); Linklist<HashMap<String,String>> L2 =NewLinklist();        SYSTEM.OUT.PRINTLN (L2); HashMap m=NewHashmap<string,string>(); M.put ("1", "1");        L2.addnode (m);        System.out.println (m);        SYSTEM.OUT.PRINTLN (L2); System.out.println (L2.getdata (0)); System.out.println (L2.getdata (1)); System.out.println (L2.getdata (2)); System.out.println (L2.getdata (4)); System.out.println (L2.getdata (5));        L2.delnode (m);        SYSTEM.OUT.PRINTLN (L2); }    }

Single-linked list Java implementation

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.