简单链表练习

class A{
    private class B{
        private B next;//**节点**
        private Object data;
        public B(Object data){
            this.data=data;
        }
        public void addB(B b){
            if(this.next==null){
                this.next=b;
            }else{
                this.next.addB(b);
            }
        }
        public void toArrayB(){//输出数组
            A.this.Redata[A.this.foot++]=this.data;
            if(this.next!=null){
                this.next.toArrayB();
            }
        }
        public boolean Contains(Object search){//检验是否存在某数据
            if(search.equals(this.data)){
                return true;
            }else{
                if(this.next!=null){
                    return this.next.Contains(search);
                }else{
                    return false;
                }
            }
        }
        public Object getB(int index){//按索引查找数据
            if(A.this.foot++==index){
                return this.data;
            }else{
                if(this.next!=null){
                    return this.next.getB(index);
                }else{
                    return false;
                }

            }
        }
        public void setB(int index,Object newData){//按索引更改数据
            if(index==A.this.foot++){
                this.data=newData;
            }else{
                if(this.next!=null){
                    this.next.setB(index, newData);
                }
            }
        }
        public void removeB(Object data,B previous){//按数据删除数组
            if(this.data.equals(data)){
                previous.next=this.next;
            }else{
                this.next.removeB(data,this);
            }
        }
    }
    //=========================
    private B root;//根目录
    private int count ;
    private int foot;
    private Object []Redata;
    public void add(Object data){//检验输入数据正确性
        if(data==null){
            return;
        }
        B b=new B(data);
        if(this.root==null){//如果没有根目录建立一个
            this.root=b;
        }else{
            this.root.addB(b);
        }
        count++;
    }
    public Object []toArray(){//根据根目录输出数组
        if(this.count==0){
            return null;
        }
        this.Redata=new Object[this.count];
        this.foot=0;
        this.root.toArrayB();
        return this.Redata;
    }
    public boolean Contain(Object search){//检验
        if(this.root==null){
            return false;
        }else{
            return this.root.Contains(search);
        }
    }
    public void set(int index,Object newData){
        if(index>=this.count){
            return;
        }else{
            this.foot=0;
            this.root.setB(index, newData);
        }
    }
    public Object get(int index){
        if(index>=this.count&&index<0){
            return null;
        }else{
            this.foot=0;

            return this.root.getB(index);
        }
    }
    public int getCount(){
        return this.count;
    }
    public void remove(Object data){
        if(this.Contain(data)){
            if(this.root.data.equals(data)){
                this.root=this.root.next;
            }else{
                this.root.next.removeB(data,this.root);
            }this.count--;
        }
    }
}
public class test3 {
    public static void main(String args[]){
        A a=new A();
        a.add(3);
        a.add(4);
        a.set(0, 1);
        a.set(1, 2);
        a.add(5);
        a.set(2, 3);
        a.remove(2);
        Object []data=a.toArray();
        for(int x=0;x<data.length;x++){
            System.out.println(data[x]);
        }
        System.out.println("===========");
        System.out.println(a.Contain(2));
        System.out.println(a.get(0));

    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值