Object Modify Linked List

Source: Internet
Author: User

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

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.