A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

范淼

初级黑马

  • 黑马币:44

  • 帖子:13

  • 精华:0

package cn.ithema02;
/**
*  定义一个MyLink的类   实现一个可变链表的增,删,改,查.
*  每个节点分为两部分 1.第一部分保存要保存到链表中的元素 2.第二部分保存下一个节点的索引
*  如果没有下一节点则第二部分为null
*/
public class MyLink {
    private class Node{         //此类的this表示的是Node类对象  内部类
        private Object data;        //保存的是Object类数据
        private Node next;          //保存下一个节点
        //Node类存在的目的就是为了保存数据,所以必须存在数据
        public Node(Object data){
            this.data = data;
        }
        //增加元素
        public void addNode(Node newNode){
            if(this.next == null){          //当前节点没有后续节点
                this.next = newNode;
            }else{
                this.next.addNode(newNode);     //递归调用  直到next指向为空  再添加元素
            }
        }
        //获得指定下标的元素
        public Object getNode(int index){
            if(MyLink.this.foot ++ == index){    //查询索引与访问脚标相等则是要找的元素
                return this.data;
            }else{
                if(this.next != null) {
                    return this.next.getNode(index);   //递归调用
                }
                return null;
            }
        }
        //判断是否包含指定元素
        public boolean containsNode(Object data){
            if(this.data.equals(data)){             //如果数据不是Object类equals 方法要覆写
                return true;
            }else{
                if(this.next == null){
                    return false;
                }
                return this.next.containsNode(data);
            }
        }
        //删除指定元素
        public void removeNode(Node node,Object data){
            if(this.data.equals(data)){
                node.next = this.next;
            }else{
                    this.next.removeNode(this,data);
            }
        }
        public void setNode(int index,Node node,Node newNode){
            if( ++ MyLink.this.foot == index){
                node.next = newNode;
                newNode.next = this;
            }else{
                this.next.setNode(index,this,newNode);
            }
        }
        //用递归遍历链表
        public void toArrayNode(){
            MyLink.this.returnDate[MyLink.this.foot ++] = this.data;
            if(this.next !=null){
                this.next.toArrayNode();
            }
        }
    }
    private Node root;          //根节点 根节点是一切的开始
    private int count;          //保存链表中数据个数
    private int foot;            //定义访问脚标  遍历链表时记录访问节点位置
    private Object []returnDate;   //对象数组 用来遍历链表存储每个节点的元素
    /**
     * 在链表结尾添加元素
     * @param data 要添加的元素
     */
    public void add(Object data) {
        if (data == null) {           //忽略了不在保存
            //方法返回值为void可以使用return直接结束调用
            return;                 //结束方法调用
        }
        Node newNode = new Node(data);  //数据封装在节点之中
        if (this.root == null) {
            this.root = newNode;    //第一个节点作为跟节点
        } else {
            this.root.addNode(newNode);
        }
        count ++;           //数据个数加一
    }
    /**
     * 返回数据个数
     * @return
     */
    public int size(){
        return this.count;
    }
    /**
     * 返回是否为空
     * @return
     */
    public boolean isEmpty(){       //是否为空    count为0 则为空链表
        return this.count == 0;
    }
    /**
     *  清空链表
     */
    public void clean(){
        this.root =null;            //根节点取消  跟节点没有数据 没有下一个节点索引
    }
    /**
     * 获得指定索引的元素
     * @param index
     * @return 为空则返回true
     */
    public Object get(int index){
        if(index > this.count){
            return null;
        }
        this.foot = 0;                      //从角标为0处开始遍历节点 逐个比较
        return this.root.getNode(index);
    }
    /**
     * 判断是否包含指定元素
     * @param data
     * @return 包含指定元素则为true
     */
    public boolean contains(Object data){
        if(this.count ==0){
            return false;
        }
        return this.root.containsNode(data);
    }
    /**
     * 删除指定元素
     * @param data 要删除的元素
     */
    public void remove(Object 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 --;
        }
    }
    /**
     * 向指定位置添加指定元素 后面元素(包括本位置元素)逐个后移
     * @param index     要添加元素的位置
     * @param data      要添加的元素
     */
    public void set(int index,Object data){
        if(index < 0 || data == null || index > this.size()){
            return;
        }
        Node newNode = new Node(data);
        if(index == 0){
            Node temp = (this.root);
            this.root = newNode;
            this.root.next = temp;
        }else if(index == this.size()){
            this.add(data);
            count --;
        } else{
           foot = 0;
           this.root.next.setNode(index,this.root,newNode);
        }
        count ++;
    }
    /**
     * 把链表中元素按顺序添加到数组中 并返回数组
     * @return  包含链表中所有元素的数组
     */
    public Object[]toArray(){
        if(this.count == 0){
            return null;
        }
        this.returnDate = new Object[this.count];
        this.foot = 0;
        this.root.toArrayNode();
        return this.returnDate;
    }
}

单向链表原理.PNG (80.72 KB, 下载次数: 0)

单向链表原理.PNG

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马