链表的简单练习和理解

*Node类负责所有的节点数据的保存和节点关系的匹配

*Link类负责控制Node类对象的产生和各节点

package com.zhizhen.demo05;

public class Link {
    private Node root;              //根节点
    private int count = 0;          //链表长度
    private int foot = 0;           //节点索引
    private Object[] retArray;      //返回的数组


    private class Node{
        private Object data;
        private Node next;

        public Node(Object data) {
            this.data = data;
        }
        /**
         * 设置新的节点
         * @param newNode 新节点对象
         * 
         * */
        public void addNode(Node newNode){
            if (next == null){
                next = newNode;
            }else {
                next.addNode(newNode);
            }
        }
        /**
         * 数据检索,判断指定数据是否存在
         * @param data 需要查询的数据
         * @return 如果查询的数据存在返回 true,否则返回false
         * */
        public boolean containsNode(Object data){
            if (this.data.equals(data)){
                return true;
            }else {
                if (next != null) {
                    return next.containsNode(data);
                }else {
                    return false;
                }
            }
        }
        /**
         *根据索引取出取出数据
         * @param index 索引编号
         * @return 返回指定索引的数据
         * */
        public Object getNode(int index){
            if (foot++ == index){
                return data;
            }else {
                return next.getNode(index);
            }
        }
        /**
         *替换指定索引的数据
         * @param data 要替换的数据 
         * @param index 要替换的位置的索引编号
         * */
        public void setNode(int index, Object data){
            if (foot++ == index){
                this.data = data;
            }else {
                next.setNode(index, data);
            }
        }
        /**
         * 删除给定数据的节点
         * @param data 要删除的数据
         * @param pre 当前节点的上一个节点
         * */
        public void removeNode(Node pre, Object data){
            if (this.data.equals(data)) {
                pre.next = next;
            }else {
                next.removeNode(this, data);
            }
        }
        /**
         *将节点中保存的数据转化为对象数组
         * */
        public void toArrayNode() {
            retArray[foot++] = data;
            if (next != null){
                next.toArrayNode();
            }
        }
    }
    
    //==================以上为内部类定义=========================
    /**
     * 链表增加数据
     * @param data 要添加的数据
     * */
    public void add(Object data){
        if (data == null){
            return;
        }
        Node newNode = new Node(data);
        if (root == null){
            root = newNode;
        }else {
            root.next.addNode(newNode);
        }
        count++;
    }
    /**
     * 返回链表的长度
     * */
    public int size(){
        return count;
    }
    /**
     * 判断链表是否为空
     * */
    public boolean isEmpty(){
        if (count == 0){
            return false;
        }else {
            return true;
        }
    }
    /**
     * 判断数据是否在链表中存在
     * */
    public boolean contains(Object data){
        if (data == null || root == null){
            return false;
        }
           return root.containsNode(data);
    }
    /**
     * 根据索引取得数据
     * */
    public Object get(int index){
        if (index > count - 1){
            return null;
        }
        foot = 0;
        return root.getNode(index);
    }
    /**
     * 使用新的内容替换指定索引的旧内容
     * */
    public void set(int index, Object data){
        if (index > count -1){
            return;
        }
        foot = 0;
        root.setNode(index,data);
    }

    /**
     * 删除指定数据,如果是对象则要进行对象比较
     * */
    public void remove(Object data){
        if (this.contains(data)){
            if (data.equals(root.data)){
                root = root.next;
            }else {
                root.next.removeNode(root,data);
            }
            count--;
        }else {
            return;
        }
    }

    /**
     * 将链表以对象数组的形式返回
     * */
    public Object[] toArray(){
        if (root == null){
            return null;
        }
        foot = 0;
        retArray = new Object[count];
        root.toArrayNode();
        return retArray;
    }
    /**
     * 清空链表
     * */
    public void clear(){
        root = null;
        count = 0;
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值