I used to learn the linked list, because the type of reception is different, each time to rewrite the linked list
Modify the list of available links below
classlink{Private classnode{PrivateObject data; PrivateNode Next; PublicNode (Object data) { This. data =data; } Public voidAdd (Node newNode) {if( This. Next = =NULL) This. Next =NewNode; Else{ This. Next.add (NewNode); } } Public voidprint () {System.out.println ( This. Data); if( This. Next = =NULL) return ; Else { This. Next.print (); } } Public BooleanContainsnode (Object data) {if(Data.equals ( This. Data)) return true; Else { if( This. Next! =NULL) return This. Next.containsnode (data); Else return false ; } } PublicObject GetNode (intindex) { if(Link. This. Foot + =index) { return This. Data; } Else { return This. Next.getnode (index); } } Public voidSetnode (intindex, Object data) { if(Link. This. Foot + =index) { This. data =data; } Else{ This. Next.setnode (Index,data); } } Public voidRemoveNode (Node Previous, Object data) {if(Data.equals ( This. Data)) {Previous.next= This. Next; } Else { This. Next.removenode ( This, data); } } Public voidToarraynode () {Link. This. Retarray[link. This. Foot + +] = This. Data; if( This. Next! =NULL) This. Next.toarraynode (); } } PrivateNode Root; Private intCount = 0 ; Private intFoot = 0 ; PrivateObject [] retarray; Public voidAdd (Object data) {if(Data = =NULL) return ; Node NewNode=NewNode (data); if(Root = =NULL) Root=NewNode; Else { This. Root.add (NewNode); } This. Count + + ; } Public voidprint () { This. foot = 0 ; if(Root = =NULL) return ; Else{root.print (); } } Public intsize () {return This. Count; } Public BooleanIsEmpty () {return This. Count = = 0 ; } Public Booleancontains (Object data) {if( This. root = =NULL|| data = =NULL) return false ; Else { return This. Root.containsnode (data); } } PublicObject Get (intindex) { if(Index > This. Count)return NULL ; Else{ return This. Root.getnode (index); } } Public voidSetintindex, Object data) { if(Index > This. Count)return ; Else{ This. foot = 0 ; This. Root.setnode (Index,data); } } Public voidRemove (Object data) {if( This. Contains (data)) { if(Data.equals ( This. Root.data)) This. root = This. Root.next; Else{ This. Root.next.removeNode (Root,data); } } } PublicObject [] ToArray () {if( This. root = =NULL){ return NULL ; } Else { This. foot = 0 ; This. Retarray =Newobject[ This. Count]; This. Root.toarraynode (); return This. Retarray; } } } Public classlink1{ Public Static voidMain (String args[]) {Link all=NewLink (); All.add (A) ; The string is converted to object All.add (B) ; All.add (C) ; All.remove (A//string has already covered the Equals () method Object [] Data=All.toarray (); for(intx = 0; x < data.length; X + +) {String str=(String) data[x];//each object is transformed down into a String System.out.println (str); } } }
Summarize:
Object class objects can accept all data types and solve the data unification problem
Object Modify Linked List