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