| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1267 人关注过本帖
标题:链表出了问题。。。
只看楼主 加入收藏
X15810803158
Rank: 2
等 级:论坛游民
帖 子:49
专家分:36
注 册:2015-2-24
结帖率:91.67%
收藏
 问题点数:0 回复次数:1 
链表出了问题。。。
class Book{//定义一个书类
    private String name;//书名
    private double price;//价钱
    public Book(String name,double price){
        this.name = name;
        this.price = price;
    }
   
    //比较
    public boolean equals(Object obj){
        if(this == obj){
            return true;
        }
        if(obj == null){
            return false;
        }
        if(!(obj instanceof Book)){
            return false;
        }
        Book book = (Book)obj;
        if(this.name.equals(book.name) && this.price == book.price){
            return true;
        }else{
            return false;
        }
        
    }
    public String toString (){
        return "书名"+this.name+" ,书价钱"+this.price;
    }
}

class Link{//定义链表
    private class Node{//内部类
        private Object date;
        private Node next;
        public Node(Object date){
            this.date = date;
        }
        public void addNode(Node newNode){
            if(this.next == null){
                this.next = newNode;
            }else{
                this.next.addNode(newNode);
            }
        }
        public boolean containsNode( Object date){
            if(this.date.equals(date)){
                return true;
            }else
                if(this.next == null){
                    return false;
                }else{
                    return this.next.containsNode(date);
                }
               
            }
        public Object getNode(int index){
            if(Link.this.foot++ == index){
                return date;
            }else{
                return this.next.getNode(index);
            }
        }
        public void removeNode(Node p,Object date){
            if(this.date.equals(date)){
                p.next = this.next;//删除当前节点
            }else{
                this.next.removeNode(this,date);
            }
        }
        public void toArrayNode(){
            Link.this.retArray[Link.this.foot++] = date;
            if(this.next != null){
                this.next.toArrayNode();
            }
        }
            
        }
   
    private Node root;//根节点
    private Node p;//上一个节点
    private int count = 0;//个数
    private int index;
    private int foot = 0;
    private Object [] retArray;
    //添加
    public void add(Object date){
        Node newNode = new Node(date);//添加数据
        if(this.root == null){//当根节点为空
            this.root = newNode;
        }else{
        this.root.addNode(newNode);
        }
        count++;
        }
    //个数
    public int size(){
        return this.count;
    }
    //是否包含
    public boolean contains(Object date){
        if(this.root == null && date == null){
            return false;
        }else{
            return this.root.containsNode(date);
        }
    }
    //查询(索引)
    public Object get(int index){
        if(index >this.count){
            return null;
        }else{
            this.foot = 0;
            return this.root.getNode(index);
        }
    }
    //删除节点
    public void remove(Object date){
        if(this.contains(date)){//包含数据
            if(this.root.date.equals(date))//根节点相符
            {
                this.root = this.root.next;//删除根节点
            }else{
                this.root.removeNode(this.root,date);//交给下一个节点
            }
        }
        this.count--;
    }
    //用数组输出
    public Object[] toArray(Object date){
        if(this.root == null){
            return null;
        }
        this.foot = 0;
        Object[] retArray = new String[this.count];//开辟组数
        this.root.toArrayNode();
        return this.retArray;
    }
}
public class LinkDe{
    public static void main(String[] args){
        Link a = new Link();
        a.add(new Book("java",12.1));
        a.add(new Book("C",13.2));
        a.add(new Book("c#",14.1));
        System.out.println(a.size());
        System.out.println(a.contains(new Book("java",12.1)));
        a.remove(new Book("java",12.1));
        System.out.println(a.get(0));
        Object [] temp = a.toArray();
    }
}
搜索更多相关主题的帖子: private Object public double return 
2017-01-03 15:25
X15810803158
Rank: 2
等 级:论坛游民
帖 子:49
专家分:36
注 册:2015-2-24
收藏
得分:0 
图片附件: 游客没有浏览图片的权限,请 登录注册
2017-01-03 15:26
快速回复:链表出了问题。。。
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.046963 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved