Java编写简单 单链表结构

 二叉树链表简单模型

package myc.java.util;

//链表类,供外界调用
public class Link {
    //内部封装类Note,进行链表的数据逻辑关系处理
    //采用内部类的原因为:Link类可以之间访问Note内部类的私有属性,并且Note对外界来说是安全的,外界无法访问
    private class Note {
        private Object data;
        private Note next;

        public Note(Object data) {
            this.data = data;
        }

        public void addNote(Object obj) {
            if (this.next == null) {
                this.next = new Note(obj);
            } else {
                this.next.addNote(obj);
            }
        }

        public boolean containsNote(Object obj) {
            if (this.next != null) {
                if (this.next.data.equals(obj)) {
                    return true;
                }
                return this.next.containsNote(obj);
            }
            return false;
        }

        public Object getNote(int index) {
            if (Link.this.foot++ == index) {
                return this.data;
            }
            return this.next.getNote(index);
        }

        public boolean setNote(int index, Object obj) {
            if (Link.this.foot++ == index) {
                this.data = obj;
                return true;
            }
            return this.next.setNote(index, obj);
        }

        public boolean removeNote(Note up, Object obj) {
            if (this.data.equals(obj)) {
                up.next = this.next;
                Link.this.count--;
                return true;
            }
            return this.next.removeNote(this, obj);
        }

        public void toArray() {
            Link.this.array[Link.this.foot++] = this.data;
            if (this.next != null) {
                this.next.toArray();
            }
        }
    }

    //-------------------------以上为内部类--------------------------------
    private Note root;//记录表头数据
    private int count;//计数
    private int foot;//脚标,控制索引
    private Object[] array;//存储数组对象

    //增加
    public void add(Object obj) {
        if (obj == null)//看个人情况,我一般链表不需要存储空数据
        {
            return;
        }
        if (root == null)//表头数据是否为空
        {
            root = new Note(obj);
        } else//如果表头数据不为空,则将数据交给Note进行处理。占用合适的Next节点
        {
            root.addNote(obj);
        }
        this.count++;
    }

    //获取数量
    public int size() {
        return this.count;
    }

    //contains判断指定的数据是否存在
    public boolean contains(Object obj) {
        if (obj == null) {
            return false;
        }
        if (obj.equals(this.root.data))//如果和root数据相等的话直接返回
        {
            return true;
        }
        return this.root.containsNote(obj);//交由Note类处理next节点数据
    }

    //判断链表是否为空
    //true表示为空
    //false表示不为空
    public boolean isEmpty() {
        return root == null || this.count == 0;
    }

    //根据索引获取数据
    public Object get(int index) {
        if (index >= this.count) {
            return null;
        }
        this.foot = 0;
        return root.getNote(index);
    }

    //根据索引修改内容
    public boolean set(int index, Object obj) {
        if (index >= this.count) {
            return false;
        }
        this.foot = 0;
        return root.setNote(index, obj);
    }

    /**
     * 删除指定数据
     * 要删除的内容是next节点,因为root已经判断过了所以直接用root.next调用,
     * 删除本义就是将要删除的对象从链表中移除,
     * 例如链表1-2-3,’-‘代表next 假设我们要删除2 最终结果就是让1-3,也就是说让2的上一节点之间指向2的下一节点
     */
    public boolean remove(Object obj) {
        if (this.count == 0 || obj == null) {
            return false;
        }
        //删除分为两种情况
        //情况一:要删除的数据是root
        if (this.root.data.equals(obj)) {
            this.root = this.root.next;//将root内容指向下一个节点
            this.count--;
        }
        return this.root.next.removeNote(this.root, obj);
    }

    //将Link数据转换为数组
    public Object[] toArray() {
        if (isEmpty())//判断如果为空的话直接返回
        {
            return null;
        }
        this.foot = 0;//初始化脚标控制索引
        this.array = new Object[this.count];//初始化数组长度为链表数量
        this.root.toArray();//调用Note类处理
        return this.array;
    }
}

测试实例如下:

package myc.util;

import myc.java.util.Link;

import java.sql.SQLOutput;
import java.util.Arrays;

public class test {
    public static void main(String[] args) {
        Link link=new Link();
        link.add("aa");
        link.add("BB");
        link.add("cc");
        System.out.println("输出链表内容:"+Arrays.toString(link.toArray()));
        System.out.println("获取索引为0的数据:"+link.get(0));
        System.out.println("获取链表数量:"+link.size());
        System.out.println("判断SS在链表中是否存在:"+link.contains("SS"));
        link.set(1,"bb");//修改索引为1的数据内容为bb
        System.out.println("输出修改后的链表内容:"+Arrays.toString(link.toArray()));
        link.remove("cc");//删除cc数据
        System.out.println("输出删除cc内容后的链表:"+Arrays.toString(link.toArray()));
    }
}

运行结果如下:

输出链表内容:[aa, BB, cc]
获取索引为0的数据:aa
获取链表数量:3
判断SS在链表中是否存在:false
输出修改后的链表内容:[aa, bb, cc]
输出删除cc内容后的链表:[aa, bb]  

记录目前学习的内容,欢迎大家讨论~

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值