JAVA实现链表

JAVA实现链表少不了递归方法,和引用传递
代码示例:
class List{
 private class Node{
 private String Data;
 private Node next;
 Node (String Data){
  this.Data=Data;
 }
 //在结点末尾加;
 public void addNode(Node newNode) {
  if(this.next==null)
   {
   this.next=newNode;
   count++;
   }
  else this.next.addNode(newNode);
 }
 //输出结点;
 public void printNode() {
  System.out.println(this.Data);
  if(this.next==null)
   return;
  else this.next.printNode();
 }
 //数据查询;
 public boolean containNode(String op) {
  if(this.Data.equals(op))return true;
  if(this.next!=null)
      this.next.Data.equals(op);
   return false;
 }
 //根据索引取得数据
 public String getIndex(int index) {
  if(foot++==index)
   {
   foot=0;
   return this.Data;
   }
  else return root.next.getIndex(index);
 }
 //修改指定数据
 public void setNode(int index,String Data) {
  if(++foot==index) {
   foot=0;
   this.Data=Data;
  }
  else root.next.setNode(index, Data);
 }
 //删除指定节点
 public void removeNode(Node previusNode,String op) {
  if(this.Data.equals(op))
   {
   previusNode.next=this.next;
   count--;
   return;
   }
  if(this.next!=null)
  this.next.removeNode(this, op);
  else System.out.println("查无此数据!!!");
 }
 //转换为数组;
 public void nodeToArry(int i) {
  Arry[i]=this.Data;
  if(this.next!=null)
   this.next.nodeToArry(++i);
 }
 public void insertNode(Node newNode,int index,int i) {
  if(i==index) {
   newNode.next=this.next;
      this.next=newNode;
      count++;
  }
  else {
  i++;
  this.next.insertNode(newNode,index,i );
  }
 }
 }
 //***以上Node是List私有内部函数,只能被List一个外部类调用;并且内部函数的成员直接被外部函数访问,不用getter,setter函数;
 private Node root;
 private int foot=0;
 public String Arry[];
 private int count=0;
 //**************************
 public void add(String UI) {
  if(UI==null)
   return;
  Node newNode=new Node(UI);
  if(root==null)
   {
   root=newNode;
   count++;
   }
  else 
   root.addNode(newNode);
 }
 //**************************
    public void print() {
     if(root==null)
      return;
     root.printNode();
    }
    //****打印长度
    public int getCount() {
     return count;
    }
    //****判断链表是否为空,空的话return true
    public boolean isEmpty() {
     if(count==0)
      return false;
     else return true;
    }
    //****数据查询
    public boolean contains(String UI) {
     if(UI==null||count==0)
      return false;
     return root.containNode(UI);
    }
    //*****索引取得数据
    public String get(int index) {
     if(index>count||index<=0)
      return null;
     else return root.getIndex(index);
    }
    //*****修改指定数据
    public void set(int index,String Data) {
     if(index>count||index<=0)
      return;
     else  root.setNode( index, Data);
    }
    //******删除根节点
    public void remove() {
     if(root.next!=null)
      {
      root=root.next;
      count--;
      }
     else
      this.clear();
    }
    //******插入结点;
    public void insert(String Data,int index) {
     if(Data==null)return;
     Node newNode=new Node(Data);
     if(index==1) {
      newNode.next=root;
      int num=++count;
      root=newNode;
      count=num;
       }
     else root.insertNode(newNode,index-1,1);
    }
    //******删除指定节点
    public void remove(String Data) {
     
      if(root.Data.equals(Data))
       this.remove();
      else {
       
       root.next.removeNode(root,Data);//root就是root.next的priviusNode;
         }
     
    }
    //*******转换为数组
    public String[] toArry() {
     if(root==null)
      return null;
     Arry=new String[this.getCount()];
     root.nodeToArry(0);
     return Arry;
    }
    //*******清空数据
    public void clear() {
     root=null;
     count=0;
    }
}
public class PrimaryNode02 {
public static void main(String args[]) { 
     List ykp=new List();
     ykp.add("ykp");
     ykp.add("MZX");
     ykp.insert("么么哒", 2);
     System.out.println(ykp.contains("ykp"));
     System.out.println(ykp.get(1));
     System.out.println(ykp.isEmpty());
     String ui[]=ykp.toArry();
     for(int i=0;i<ui.length;i++)
      System.out.println(ui[i]);
     ykp.remove();
     ykp.remove("ykp");
     ykp.remove("MZX");
     ykp.print();
     ykp.clear();
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值