Javaチェーンの定義と簡単な例


Javaチェーンの定義と簡単な例
Javaは連鎖表を実現して主に引用伝達に頼って、引用は住所として理解することができて、連鎖表のは遍歴して多く再帰を使って、ここで私は1つの疑問が存在して同じ種類の異なった対象の同じ方法の中で呼び出して再帰を計算しません。
ここで私が書いたのは一方向チェーンです。

package com.example.java;

public class MyLink {

public static void main(String [] args){ 

Link l=new Link(); 
  mytype[] la; 
  mytype dsome=new mytype("  ","dsome",21); 
  mytype shao=new mytype("  ","john",45); 
  mytype hua=new mytype("   ","jam",46); 
  mytype duo=new mytype("   ","duo",1000); 
  mytype wang=new mytype("  ","jack",21); 
  mytype shi=new mytype("  ","bob",3000); 
  mytype yu=new mytype("  ","keven",30); 

l.add(dsome);//       
  l.add(shao); 
  l.add(hua); 
  l.add(wang); 
  l.add(shi); 
  l.add(duo); 
  l.add(yu); 

  System.out.println("    :"+l.length());//     
  la=l.toArray(); 
  for(int i=0;i<la.length;i++){ 
 System.out.println(la[i].getInfo()); 
 } System.out.println("      :"+l.contains(duo)+"
"); System.out.println("
"); l.remove(duo); la=l.toArray(); for(int i=0;i<la.length;i++){// System.out.println(la[i].getInfo()); } System.out.println("
"); for(int i=0;i<l.length();i++){ System.out.println(l.get(i).getInfo()); } System.out.println(" :"+l.contains(duo)+"
"); l.clean(); System.out.println(" : "+l.length()+"\t :"+l.isEmpty()); } }

package com.example.java;
public class Link {

private class Node{//    
private Node next; 
private mytype data; 
public Node(mytype data){ 
   this.data=data; 
 } 

public void addNode(Node newNode){//     
   if(this.next==null){ 
    this.next=newNode; 
   }else{ 
    this.next.addNode(newNode); 
   } 
  } 

  public mytype getNode(int index){//         

   if(index==Link.this.foot++){ 
    return this.data; 
   }else{ 
    return this.next.getNode(index); 
   } 
  } 

  public boolean iscontain(mytype data){//          
   if(this.data.equals(data)){ 
    return true; 
   }else{ 
    if(this.next!=null){ 
     return this.next.iscontain(data); 
    }else{ 
     return false; 
    } 
   } 
  } 

  public void removeNode(Node previous,mytype data){//     
   if(this.data.equals(data)){ 
    previous.next=this.next; 

   }else{ 
    this.next.removeNode(this,data); 
   } 
  } 


  public void toArrayNode(){//     
    Link.this.Larray[Link.this.foot ++]=this.data; 
    if(this.next!=null){ 
     this.next.toArrayNode(); 
    } 
   }  
}

//        
private Node root; 
private int count=0; 
private int foot; 
private mytype [] Larray;

public void add(mytype data){//     
  if(data==null){ 
   System.out.print("      ,    ");//    
   return; 
  } 
  Node newNode=new Node(data); 
  if(this.root==null){ 
   this.root=newNode; 
   this.count++; 
  }else{ 
   this.root.addNode(newNode); 
   this.count++; 
  } 
 } 

 public int length(){//     
  return this.count; 
 } 

 public boolean isEmpty(){//       
  if(this.count==0)return true; 
  else return false; 
 } 

 public void clean(){//     
  this.root=null; 
  this.count=0; 
 } 

 public mytype get(int index){//            
    if(index>=this.count||index<0){ 
     System.out.print("    ");//    
     return null; 
    }else{ 
     this.foot=0; 
     return this.root.getNode(index); 
    } 
   } 

   public boolean contains(mytype data){//         data 
    if(data==null) 
     return false; 
    return this.root.iscontain(data); 
   } 

   public void remove(mytype data){//         
    if(this.contains(data)){ 
     if(this.root.data.equals(data)){ 
      this.root=this.root.next; 
      this.count--;   
     } 
     else{ 
      this.count--; 
      this.root.next.removeNode(root,data); 
     } 
    }else{ 
     System.out.print("    ");//        
    } 
   } 

   public mytype[] toArray(){//           
    if(this.count==0){ 
     return null; 
    } 
     this.foot=0; 
     this.Larray=new mytype [this.count]; 
     this.root.toArrayNode(); 
     return this.Larray;     
   }     
}


package com.example.java;

public class mytype {

private String name; 
private String people; 
private int age;

public mytype(String name,String people,int age){//      (    ) 
  this.name=name; 
  this.people=people; 
  this.age=age; 
 } 
 public boolean equals(mytype data){//         
  if(this==data){ 
   return true; 
  } 
  if(data==null){ 
   return false; 
  } 
  if(this.name.equals(data.name)&&this.people.equals(data.people)&&this.age==data.age){ 
   return true; 
  }else{ 
   return false; 
  } 
 }
public String getName() {
  return name;
}
public void setName(String name) {
  this.name = name;
}
public String getPeople() {
  return people;
}
public void setPeople(String people) {
  this.people = people;
}
public int getAge() {
  return age;
}
public void setAge(int age) {
  this.age = age;
} 

public String getInfo(){ 
  return "   :"+this.name+"
"+ " :"+this.people+"
"+ " :"+this.age; } }
テストの効果は以下の通りです。

    :7 
   :   
   :dsome 
   :21 
   :   
   :john 
   :45 
   :    
   :jam 
   :46 
   :   
   :jack 
   :21 
   :   
   :bob 
   :3000 
   :    
   :duo 
   :1000 
   :   
   :keven 
   :30 
      :true

     

   :   
   :dsome 
   :21 
   :   
   :john 
   :45 
   :    
   :jam 
   :46 
   :   
   :jack 
   :21 
   :   
   :bob 
   :3000 
   :   
   :keven 
   :30

             
   :   
   :dsome 
   :21 
   :   
   :john 
   :45 
   :    
   :jam 
   :46 
   :   
   :jack 
   :21 
   :   
   :bob 
   :3000 
   :   
   :keven 
   :30 
      :false

           : 0       :true