This is a simple implementation of the LinkedList bottom.
Packagecom.yck.mylinkedlist; Public classNode {PrivateNode previous;//Previous Node PrivateObject object;//What is stored at this node ? PrivateNode Next;//next node. PublicNode () {} PublicNode (node Previous, Object object, node next) {Super(); This. Previous =previous; This. Object =object; This. Next =Next; } PublicNode getprevious () {returnprevious; } Public voidsetprevious (Node previous) { This. Previous =previous; } PublicObject GetObject () {returnobject; } Public voidSetObject (Object object) { This. Object =object; } PublicNode GetNext () {returnNext; } Public voidSetnext (Node next) { This. Next =Next; } }
Packagecom.yck.mylinkedlist;/*** Manual implementation of the LinkedList bottom, this time with generics *@authorAdministrator * *@param<T>*/ Public classMylinkedlist<t>{ PrivateNode first; PrivateNode last; Private intsize; Publicmylinkedlist () {} Public voidAdd (T t) {Node n=NewNode (); if(First = =NULL) {n.setprevious (NULL); N.setobject (t); N.setnext (NULL); First=N; Last=N; Size++; } Else{n.setprevious (last); N.setobject (t); N.setnext (NULL); Last.setnext (n); Last=N; Size++; } } Public voidAddintindex,t T) {Rangcheck (index); Node Temp=NewNode (); if(Index = = 0) {temp.setprevious (NULL); Temp.setobject (t); Temp.setnext (first); First=temp; } Else if(Index! =size) {temp.setprevious (getnode (Index-1)); Temp.setobject (t); Temp.setnext (GetNode (index)); GetNode (Index-1). Setnext (temp); GetNode (Index). setprevious (temp); } Else{temp.setprevious (last); Temp.setobject (t); Temp.setnext (NULL); Last=temp; } size++; } Public voidAdd (mylinkedlist<t>list) {Node temp=NewNode (); Temp=List.first; Temp.setprevious ( This. last); This. Last.setnext (temp); This. Last =List.last; This. Size + =list.size; List.first.setPrevious (NULL); } Public voidRemoveintindex) {GetNode (Index-1). Setnext (GetNode (index+1));; GetNode (Index). Setprevious (GetNode (Index-1)); Size--; } PublicNode GetNode (intindex) {Rangcheck (index); Node Temp=NewNode (); Temp=First ; for(inti=0;i<index;i++) {Temp=Temp.getnext (); } returntemp; } PublicObject Get (intindex) { returngetnode (Index). GetObject (); } Public voidRangcheck (intindex) { if(index<0 | | index>= This. Size)Try { Throw NewException (); } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); } } Public Static voidMain (string[] args) {mylinkedlist<String> list =NewMylinkedlist<string>(); List.add ("AAA"); List.add ("Ooo"); List.add ("XXX"); List.add ("FFF"); for(inti=0;i<list.size;i++) System.out.println (List.get (i)); System.out.println ("****************"); List.remove (2); for(inti=0;i<list.size;i++) System.out.println (List.get (i)); System.out.println ("****************"); Mylinkedlist<String> List1 =NewMylinkedlist<string>(); List1.add ("I"); List1.add ("Love"); List1.add ("U"); System.out.println (List1.first.getPrevious ()); System.out.println ("****************"); for(inti=0;i<list1.size;i++) System.out.println (String) list1.get (i)); System.out.println ("****************"); List.add (List1); for(inti=0;i<list.size;i++) System.out.println (String) list.get (i)); System.out.println ("****************"); }}
Almost forgot to post the results of the simple test.
Simple implementation of the LinkedList of Javase Foundation