注册 登录
编程论坛 JAVA论坛

检查了半天 想不出

X15810803158 发布于 2016-12-06 13:54, 1562 次点击


class Link{
    private class Node{
        private String date;//数据
        private Node next;//下一个节点
        public Node(String date){
            this.date = date;
        }
        public void addNode(Node newNode){
            if(this.next == null){
                this.next = newNode;
            }else{
                this.next.addNode(newNode);
               
            }
            
        }
        public boolean containsNode(String date){//查询
            if(date.equals(this.date)){
                return true;
            }else{
                if(this.next != null){
                    return this.next.containsNode(date);
                }else{
                    return false;
                }
            }
        }
    }
    private Node root;//根节点
   
    public void add(String date){
        Node newNode = new Node(date);//把数据封装起来
        if(this.root == null){
            this.root = newNode;
        }else{
            this.root.addNode(newNode);
            
        }
        
        public boolean contains(String date){
            if(date == null ){
                return false;
            }
                return this.root.containsNode(date);
            
        }
        
    }
   
}

public class LinkDemo{
   
    public static void main(String[] args){
        Link all = new Link();
        all.add("h");
        all.add("e");

        System.out.println(all.contains("h"));
    }
}
3 回复
#2
孤独与烈酒2016-12-06 17:12
你的contains方法写在add方法里了 肯定报错
#3
kaosaier2016-12-06 19:10
回复 2楼 孤独与烈酒
dui
#4
GrayJerry2016-12-16 17:09
2l正解
1