Java链表的定义与简单实例

  

<强>,Java链表的定义与简单实例

  

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

  

这里我写的是单向链表;

        包com.example.java;      公开课MyLink {      公共静态void main (String [] args) {      链接l=new链接();   mytype[]拉;   mytype dsome=new mytype(“韩敏”、“dsome”, 21);   mytype邵=new mytype(“邵晓”,“约翰”,45);   mytype华=new mytype(“华晓风”、“堵塞”,46名);   mytype两=new mytype(“余小风”,“二”,1000年);   王mytype=new mytype(“王秋”,“杰克”,21);   施mytype=new mytype(“韩寒”,“bob”, 3000年);   mytype yu=new mytype(“于冬”,“凯文”,30);      l.add (dsome);//测试增加节点   l.add(邵);   l.add(华);   l.add(王);   l.add (shi);   l.add(二);   l.add (yu);      System.out.println(“链表长度:“+ l.length());//链表长度   la=l.toArray ();   for (int i=0; i之前            包com.example.java;   公共类链接{      {//私有类节点内部类   私人节点下;   私人mytype数据;   公共节点(mytype数据){   this.data=https://www.yisu.com/zixun/data;   }      公共空间addNode(节点newNode){//增加节点   如果(this.next==null) {   this.next=newNode;   其他}{   this.next.addNode (newNode);   }   }      公共mytype getNode (int指数){//按照角标返回数据      如果(指数==Link.this.foot + +) {   返回this.data;   其他}{   返回this.next.getNode(指数);   }   }      公共布尔iscontain (mytype数据){//判断是否含有该数据   如果(this.data.equals(数据)){   返回true;   其他}{   如果(this.next !=null) {   返回this.next.iscontain(数据);   其他}{   返回错误;   }   }   }      公共空间removeNode(节点之前,mytype数据){//删除节点   如果(this.data.equals(数据)){   previous.next=this.next;      其他}{   this.next.removeNode(数据);   }   }         公共空间toArrayNode(){//转化数组   Link.this.Larray [Link.this。脚+ +]=this.data;   如果(this.next !=null) {   this.next.toArrayNode ();   }   }   }   之前         //内部类定义完毕   私人根节点;   私人int数=0;   私人int脚;   私人mytype [] Larray;      公共空间添加(mytype数据){//增加节点   如果(data=https://www.yisu.com/zixun/=null) {   System.out.print(“增加数据失败,数据为空");//测试用   返回;   }   节点newNode=新节点(数据);   如果(this.root==null) {   this.root=newNode;   this.count + +;   其他}{   this.root.addNode (newNode);   this.count + +;   }   }      公共int长度(){//链表长度   返回this.count;   }      公共布尔isEmpty(){//是否为空链表   如果(this.count==0)返回true;   否则返回假;   }      公共空间干净(){//清空链表   this.root=零;   this.count=0;   }      公共mytype (int指数){//索引返回节点所存的数据   如果(index>=this.count | | index<0) {   System.out.print(“越界错误");//测试用   返回null;   其他}{   this.foot=0;   返回this.root.getNode(指数);   }   }      公共逻辑包含(mytype数据){//判断链表数据是否含数据   如果(data=https://www.yisu.com/zixun/=null)   返回错误;   返回this.root.iscontain(数据);   }      公共空间删除(mytype数据){//删除指定数据节点   如果(this.contains(数据)){   如果(this.root.data.equals(数据)){   this.root=this.root.next;   this.count——;   }   其他{   this.count——;   this.root.next.removeNode(根、数据);   }   其他}{   System.out.print(“删除错误");//测试用   }   }      公共mytype [] toArray(){//把链表转化成对象数组   如果(this.count==0) {   返回null;   }   this.foot=0;   这一点。Larray=new mytype [this.count];   this.root.toArrayNode ();   返回this.Larray;   }   }      

Java链表的定义与简单实例