Storage and storage array linked list

data structure

Array achieve

Thinking, into Objiect class every once credited for time, when the array is not enough time to determine needs and expansion

Implement CRUD

 

Package cn.jiedada._02inArray; 

Import java.util.Arrays; 

/ * 
 * into arbitrary data 
 * size expansion object class 
 * print we want 
 * achieve CRUD 
 * * / 
public  class inArray { 
    Object [] DATE ; 
    int IndexSize = 0 ;
     int captipy;
     // call the method of their 
    public inArray () {
         the this (. 5 ); 
    } 
    public inArray ( int captipy) {
         the this .captipy = captipy; 
        DATE = new new Object [captipy]; 
    }
    public void add(Object obj){
        //对数组进行扩容
        if(indexsize>date.length-1){
            Object[] newdate=new Object[indexsize+20];
            System.arraycopy(date, 0, newdate, 0, indexsize);
            date=newdate;
        }
        date[indexsize]=obj;
        indexsize++;
    }
    /*覆写String
     * */
    @Override
    public String toString() {
        Object[] object = newObject [IndexSize]; 
        System.arraycopy (DATE, 0, Object, 0 , IndexSize);
         return of Arrays.toString (Object); 
    } 
    Private  void judje ( int index) {
         / * IF (index <0 | index> IndexSize) { 
            System.out.println ( "Please enter the correct number of" + IndexSize); 
        } * / 
        IF (index <0 | index> IndexSize)
         the throw  new new An ArrayIndexOutOfBoundsException ( "Please enter the correct number ~ 0" + IndexSize); 
    }     // query 
    public Object selectElemple ( int index) { 
        judje (index);
        return date[index];
    }
    //查询
    public int selectNameElement(String name){
        for(int i=0;i<date.length;i++){
            if(date[i].equals(name)){
                return i;
            }
        }
        return -1;
    }
    //删除
    public void deleteElement(int index){
        judje(index);
        System.arraycopy(date, index+1, date, index,indexsize-index-1);
        indexsize--;    
    }
    public void deleteElement(String name){
        int index = selectNameElement(name);
        deleteElement(index);
    }

}
View Code

 

 

 

List implementation

Definition: the need to store a next value data objects and a list of queries need to be traversed , by the class name next to store the address value of the next position,

 

Package cn.jiedada._03LinkList; 

public  class LinkList { 
    the Node Frist; // Create head pointer 
    public  void the Add (Object Object) { 
        the Node Node = new new the Node (Object);
         // determine whether the head pointer 
        IF (Frist == null ) { 
            Frist = node; 
        } the else { 
            the node TEMP = Frist;
             // loop through the next node, the last node to find 
            the while (temp.next =! null ) { 
                TEMP =  temp.next;
            }
            // value stored 
            temp.next = Node; 
        } 
    } 
    @Override 
    public String toString () { 
        the StringBuilder StringBuilder = new new the StringBuilder ( "[" ); 
        the Node TEMP; 
        TEMP = Frist;
         IF (TEMP == null ) {
             return  null ; 
        } 
        // loop through, because the first to determine whether the value if the value does not exit 
        the while (TEMP! = null ) {
             // next judgment is not a null value. 
        IF (temp.next! = null) {
            StringBuilder.Append (temp.object) .append ( "," );
             // Why this can not temp.next = TEMP; 
        } the else { 
            StringBuilder.Append (temp.object) .append ( "]" ); 
        } 
        TEMP = temp.next; 
    } 
        return StringBuilder.ToString (); 
    } 
    
} 
// list the value of the address 
class the Node { 
    Object Object; 
    the Node next; // next stored address value 
    public the Node () {
         // the TODO Auto-Generated constructor Stub 
    }
     public Node(Object obj) {
        this.object=obj;
    }
}
View Code

 

Guess you like

Origin www.cnblogs.com/xiaoruirui/p/11302565.html