当前位置: 移动技术网 > IT编程>开发语言>Java > java 中链表的定义与使用方法

java 中链表的定义与使用方法

2019年07月22日  | 移动技术网IT编程  | 我要评论

java 中链表的定义与使用方法

java实现链表主要依靠引用传递,引用可以理解为地址,链表的遍历多使用递归,这里我存在一个疑问同一个类的不同对象的的相同方法的方法内调用算不算递归.

这里我写的是单向链表;

实例代码:

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)+"\n"); 
  system.out.println("删除多余后\n"); 
  l.remove(duo); 
  la=l.toarray(); 
  for(int i=0;i<la.length;i++){//转化为数组之后输出 
   system.out.println(la[i].getinfo()); 
  }  
system.out.println("\n利用索引方法输出全部数据"); 
  for(int i=0;i<l.length();i++){ 
   system.out.println(l.get(i).getinfo()); 
  }  
system.out.println("是否包含多余:"+l.contains(duo)+"\n"); 
  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+"\n"+ 
      "人物 :"+this.people+"\n"+ 
      "年龄 :"+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

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网