Java学习笔记之--实现链表的各种操作


package 继承类;

interface ILink<E> {        //避免出现classcastException安全隐患
    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);  //删除你看着不顺眼的元素
}

class LinkImpl<E> implements ILink<E> {
    //定义用于返回数组声明和数组索引的控制属性
    private int foot; //对象数组的索引
    private Object returnData[];  //用来获取链表的data的对象数组

    class Node<E> {    //为外部类提供服务
        private E data;   //结点保存数据
        private Node<E> next;   //结点保存引用
        public Node(E data) {
            this.data = data;
        }
        public void addNode(Node<E> newNode) {
            if (this.next == null) {
                this.next = newNode;
            } else {
                this.next.addNode(newNode);
            }
        }
        //我们要通过递归的方法将链表中的数据保存到数组当中
        public void toArrayNode() {
            LinkImpl.this.returnData[LinkImpl.this.foot++] = this.data;
            if(this.next!= null) {
                this.next.toArrayNode();  //说明了还有数据需要去取出来
            }
        }
        //追加结点索引获取数据
        public E getNode(int index) {
            if(LinkImpl.this.foot++ == index) {
                return this.data;  //数据类型为泛型
            } else {
                return this.next.getNode(index);  //否则继续往下找
            }
        }
        //追加修改链表元素的方法
        public void setNode(int index, E data) {
            if(LinkImpl.this.foot++ == index) {
                this.data = data;   //修改数据
            }else {
                this.next.setNode(index,data);  //后续的结点操作
            }
        }
        //追加对链表内容查询的方法
        public boolean containsNode(E data) {
            if(this.data.equals(data)) {  //就是对你给的元素 与链表中的元素比较是否相等
                return true;
            }
            else {
                if(this.next == null) {
                    return false;
                } else {
                    return this.next.containsNode(data);
                }
            }
        }
        public void removeNode(Node previous, E data) {
            if(this.data.equals(data)) { //就是我们想要删除元素跟 链表 中的元素比较 找的到的才能删除
                previous.next = this.next;
            } else {
                if(this.next!=null) {
                    this.next.removeNode(this,data);
                }
            }
        }
    }
    //下一步 在IlinkImpl子类当中实例化toarray数组
    @Override
    public Object[] toArray() {
        if(this.isEmpty()) {
            throw new NullPointerException("抛出一个集合为空的异常");
        }
        this.foot=0;  //首先将索引置为0
        this.returnData = new Object[this.count];  //通过链表count元素个数来开辟动态数组的空间
        this.root.toArrayNode();
        return this.returnData;
    }
    public int size() {
        return count;
    }
    private Node<E> root;
    private int count;
    //-----------------link--------------
    public void add(E e) {  //第一步 复写add()方法
        if (e == null) {  //表示保存的数据为null时 返回空
            return;
        }
        Node<E> newNode = new Node<E>(e);   //第二步 新建一个结点
        if (this.root == null) {
            this.root = newNode;  //如果根节点为空 那么新开辟的结点就是根节点
        } else {
            this.root.addNode(newNode);  //由Node类保存新的结点
        }
        count++;
    }
    public boolean isEmpty() {
        if(count==0)
            return true;
        // if(root == null) return true;
        else {
            return false;
        }
    }
    @Override
    public E get(int index) {
        if(index >= this.count) {
            throw new ArrayIndexOutOfBoundsException("超出了链表数据的范围");
        }
        this.foot=0;
        return this.root.getNode(index); //交给Node类来查找
    }
    @Override
    public void set(int index, E data) {
        if(index>=this.count) {
            throw new ArrayIndexOutOfBoundsException("超出范围 无法修改数据");
        }
        this.foot=0;
        this.root.setNode(index,data);  //交给内部类来帮我实现这个功能
    }
    @Override
    public boolean contains(E data) {
        if(data == null) {
            return false;
        }
        return this.root.containsNode(data);
    }
    @Override
    public void remove(E data) {
        if(this.contains(data)) {
            if(this.root.data.equals(data)) {
                this.root = this.root.next;
            } else {
                this.root.next.removeNode(this.root,data);
            }
            this.count--;
        }
    }
}
public class Java11 {
    public static void main(String[] args) {
        ILink<String> link = new LinkImpl<String>();
        System.out.println("开始结点的个数" + link.size());
        System.out.println("结点是否为空" + link.isEmpty());
        link.add("www.baidu.com");
        link.add("www.syhgdx.com");
       Object result[]  = link.toArray();
       for(Object obj: result) {
           System.out.println(obj + "、");
       }
       System.out.println(link.contains("www.baidu.com"));
       System.out.println("结束的结点个数" + link.size());
       System.out.println("结点是否为空" + link.isEmpty());
       link.set(1,"sssssss");
       System.out.println(link.get(1));
    }
}

执行结果如下
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值