链表的学习

interface ILink<E>{
    public void add(E e);
    public int size();
    public boolean isEmpty();
    public Object [] toArray();
    public E get(int index);
    public void set(int index,E data);
    public boolean contains(E data);
    public void remove(E data);
    public void clean();
}
class ILinkImpl<E> implements ILink<E>{
    private class Node{
        private Node next;
        private E data;
        public Node(E data){
            this.data = data;
        }
        public void addNode(Node newNode){
            if(this.next == null){
                this.next = newNode;
            }else{
                this.next.addNode(newNode);//保存节点
            }
        }
        public void toArrayNode(){
            ILinkImpl.this.returnData[ILinkImpl.this.foot ++] = this.data;
            if(this.next != null){
                this.next.toArrayNode();
            }
        }
        public E getNode(int index){
            if(ILinkImpl.this.foot ++ == index){
                return this.data;
            }else{
                return this.next.getNode(index);
            }
        }
        public void setNode(int index,E data){
            if(ILinkImpl.this.foot ++ == index){
                this.data = data;
            }else{
                this.next.setNode(index,data);
            }
        }
        public boolean containsNode(E data){
            if(data.equals(this.data)){
                return true;
            }else{
                if(this.next != null){
                    return this.next.containsNode(data);
                }else{
                    return false;
                }
            }
        }
        public void removeNode(Node previous,E data){
            if(this.data.equals(data)){
                previous = this.next;
            }else{
                this.next.removeNode(this,data);
            }
        }
    }
    private Node root;
    private int count;
    private int foot;
    private Object [] returnData;
    public void add(E e){
        if(e == null){
            return ;
        }
        Node newNode = new Node(e);
        if(this.root == null){
            this.root = newNode;
        }else{
            this.root.addNode(newNode);//保存数据
        }
        this.count ++;
    }
    public int size(){
        return this.count;
    }
    public boolean isEmpty(){
        return this.root == null;
    }
    public Object [] toArray(){
        if(this.root == null){
            return null;
        }
        this.foot = 0;
        this.returnData = new Object[this.count];
        this.root.toArrayNode();
        return this.returnData;
    }
    public E get(int index){
        if(index >= this.count){
            return null;
        }
        this.foot = 0;
        return this.root.getNode(index);
    }
    public void set(int index,E data){
        if(data == null){
            return ;
        }
        this.foot = 0;
        this.root.setNode(index,data);
    }
    public boolean contains(E data){
        if(data == null){
            return false;
        }
        return this.root.containsNode(data);
    }
    public void remove(E data){
        if(this.contains(data)){
            if(this.root.data.equals(data)){
                this.root = this.root.next;
            }else{
                this.root.removeNode(this.root,data);
            }
            this.count --;
        }
    }
    public void clean(){
        this.count = 0;
        this.root = null;
    }
}

以上是链表代码。

分别是:增加数据、获取集合长度、判断是否为空、以数组的方式返回集合信息、根据索引获取数据信息、根据索引去修改数据信息、给出信息查看链表中是否存在该信息,删除信息、清空集合。

大概思路:

1,在增加数据信息之前,需要判断是否存在根节点,存在了,直接增加信息即可,如果不存在,则新开辟的节点就是根节点,增加下一个节点的数据信息也是需要进行判断和增加的,可以进行递归调用;

2,集合长度就是没增加一个信息或者每减少一个信息所得到的count数量;

3,没有根节点或者长度为0就是空;

4,将集合以数组的方式返回并遍历;

5,进行脚标的判断;

5,脚标判断以后,相等则修改数据信息;

6,内容的判断,equals()方法实现;

7,在内容判断的基础之上,进行数据信息的删除;

8,清空集合,根节点为空,集合长度为0,就是清空集合。

如有大神看了之后,需要补充的,麻烦留言说明,感谢!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值