美文网首页
链表--java简单实现

链表--java简单实现

作者: 地表最强程序员小白 | 来源:发表于2017-11-27 19:44 被阅读10次
public class LinkDemo {
    public static void main(String[] args){
        Link all=new Link();
        all.add(new Book("Java开发实战经典",79.8));
        all.add(new Book("Oracle开发实战经典",89.8));
        all.add(new Book("Android开发实战经典",99.8));
        System.out.println("保存书的个数:"+all.size());
        System.out.println(all.contains(new Book("Java开发实战经典",79.8)));
        all.remove(new Book("Android开发实战经典",99.8));
        Book [] books=all.toArray();
        for(int x=0;x<books.length;x++){
            System.out.println(books[x].getInfo());
        }
    }
}

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){
        if(book==null){
            return false;
        }
        if(this==book){
            return true;
        }
        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 {
    private class Node {
        private Book data;
        private Node next;

        public Node(Book data) {
            this.data = data;
        }

        public void addNode(Node newNode) {
            if (this.next == null) {
                this.next = newNode;
            } 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){
            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){
        if(data==null){
            return;
        }
        Node newNode=new Node(data);
        if(this.root==null){
            this.root=newNode;
        }
        else{
            this.root.addNode(newNode);
        }
        this.count++;
    }
    public int size(){
        return this.count;
    }
    public boolean isEmpty(){
        return this.count==0;
    }
    public boolean contains(Book data){
        if(data==null || this.root==null){
            return false;
        }
        return this.root.containsNode(data);
    }
    public Book get(int index){
        if(index>this.count){
            return null;
        }
        this.foot=0;
        return this.root.getNode(index);
    }
    public void set(int index,Book data){
        if(index>this.count){
            return;
        }
        this.foot=0;
        this.root.setNode(index,data);
    }
    public void remove(Book data){
        if(this.contains(data)){
            if(data.equals(this.root.data)){
                this.root=this.root.next;
            }
            else{
                this.root.next.removeNode(this.root,data);
            }
            this.count--;
        }
    }
    public Book[] toArray(){
        if(this.root==null){
            return null;
        }
        this.foot=0;
        this.retArray=new Book[this.count];
        this.root.toArrayNode();
        return this.retArray;
    }
    public void clear(){
        this.root=null;
        this.count=0;
    }
}

相关文章

  • 链表

    单链表 C实现 Java实现 双链表 C实现 Java实现

  • 链表

    一、单向链表 单向链表的普通实现 Java实现: Kotlin实现: 单向链表的递归实现 Java实现: 二、双向...

  • 链表--java简单实现

  • java参照linux内核链表的实现方式

    链表是一种很简单的数据结构,java里面的LinkedList提供了具体实现 通常大部分链表都是这样的实现方式,将...

  • 基于动态数组的实现 Java实现 基于链表的栈的实现 Java实现

  • 队列

    基于数组的循环队列 Java实现 基于链表的队列实现 Java实现

  • Java实现简单的链表-面向初学者

    很久之前用C语言实现过链表,现在已经太久没用C语言。就先用JAVA实现一个简单链表好了,还是使用最原始的C语言实现...

  • Java数据结构(四):线性表之双向链表

    java实现简单的双向链表,双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直...

  • 15反转链表

    题目描述 输入一个链表,反转链表后,输出新链表的表头。 Java实现

  • 链表面试题Java实现【重要】

    链表面试题Java实现【重要】

网友评论

      本文标题:链表--java简单实现

      本文链接:https://www.haomeiwen.com/subject/bkzdbxtx.html