java链表整理

使用的内部与外部类的结合写的链表

package head_first;

class Book{
    private String title;//书名
    private double price;//价格

    //构造方法
    public Book(String title,double price) {
        this.title = title;
        this.price = price;
    }

    //判断分三种情况
    public boolean compare(Book book) {
        //1.当book是空时
        if (book == null)
            return false;
        //2.当book与自己比较时
        if(book == this)
            return false;
        //3.当各个属性相同时
        if(this.title.equals(book.title) && this.price == book.price)
            return true;
        else
            return false;
    }

    //获取书籍信息
    public String getInfo() {
        return "图书名称:"+this.title+"图书价格:"+this.price;
    }
}

class Link{

    //内部类Node
    private class Node{
        //每个链表元素都有一本书
        private Book data;
        //下一链表节点
        private Node next;

        //每一个Node类对象都必须保存相应的数据
        private Node(Book data) {
            this.data = data;
        }

        //更加新的节点
        public void addNode(Node newNode) {
            //当前下一节点为null时
            if(this.next == null) 
                this.next = newNode;
            //当下一节点不是null继续next
            else
                this.next.addNode(newNode);
        }

        //判断该元素存在
        public boolean containsNode(Book data) {
            //
            if(data.compare(this.data))
                return true;
            else {
                if(this.next != null)
                    return this.next.containsNode(data);
                else
                    return false;
            }
        }

        //获取节点编号
        public Book getNode(int index) {
            if(Link.this.foot++ == index)
                return this.data;
            else
                return this.next.getNode(index);
        }

        //设置编号
        public void setNode(int index,Book data) {
            if(Link.this.foot++ == index)
                this.data = data;
            else
                this.next.setNode(index,data);
        }

        //传入根节点和要删除的节点
        public void removeNode(Node previous,Book data) {
            //1.直接从根节点判断元素
            if(data.compare(this.data))
                previous.next = this.next;

            else
                this.next.removeNode(this,data);
        }

        //将链表中的数据转化为对象数组输出
        public void toArrayNode() {
            Link.this.retArray[Link.this.foot++] = this.data;
            if(this.next != null)
                this.next.toArrayNode();
        }
    }

    //根节点
    private Node root;
    //保存节点个数
    private int count = 0;
    //节点索引
    private int foot = 0;
    //返回的数组
    private Book[]retArray;
    //增加节点(分三中情况)

    public void add(Book data) {
        //1.当增加的节点是空
        if (data == null)
            return;
        //2.当根节点是空时
        Node newNode = new Node(data);
        if(this.root == null)
            this.root = newNode;
        //3.当根节点不是空时,我们找到她为空的节点
        else
            this.root.addNode(newNode);
        //节点个数+1
        this.count++;
    }

    //节点个数(链表的长度)
    public int size() {
        return this.count;
    }

    //判断链表是否为空
    public boolean isEmpty() {
        return this.count == 0;
    }

    //判断数据是否存在
    public boolean contains(Book data) {
        //1.该节点是都为空或者这个链表是都为空
        if(data == null || this.root ==null)
            return false;
        return this.root.containsNode(data);
    }

    //按照索引查找书籍
    public Book get(int index) {
        //1.判断是否超过了链表长度
        if(index >this.count)
            return null;
        //2.没超过就初始化索引
        this.foot = 0;
        //getNode(index)是内部类Node的方法所以我们使用的this.root.getNode(index);
        return this.root.getNode(index);
    }

    //按照索引修改数据
    public void set(int index,Book data) {
        if(index > this.count)
            return ;
        this.foot = 0;
        //与上一个this.root.getNode(index);相似
        this.root.setNode(index, data);
    }

    //链表数据的删除操作
    public void remove(Book data) {
        //1.首先判断这个是否存在
        if(this.contains(data)) {
            if(data.equals(this.root.data))
                this.root = this.root.next;
            else
                this.root.next.removeNode(this.root,data);
            //存在就count-1
            this.count--;
        }
    }

    //将链表中的数据转化为对象数组输出
    public Book[] toArray() {
        //1.当根节点为空时
        if(this.root == null)
            return null;
        //2.初始化索引
        this.foot = 0;
        //根据保存内容开辟数组
        this.retArray = new Book[this.count];
        //交给Node类处理
        this.root.toArrayNode();
        //返回数组对象
        return this.retArray;
    }

    //清空链表
    public void clear() {
        //将根节点清空
        this.root = null;
        //元素个数为0
        this.count = 0;
    }
}

public class Exp3134 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Link all = new Link();
        all.add(new Book("java开发",79.5));
        all.add(new Book("android开发",32.1));
        all.add(new Book("c++开发",23.4));

        System.out.println("保存书的个数:" + all.size());
        System.out.println(all.contains(new Book("java开发",79.5)));

        all.remove(new Book("andriod开发",11.3));
        Book [] books = all.toArray();
        for(int x=0;x<books.length;x++)
            System.out.println(books[x].getInfo());
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值