java吧 关注:1,230,646贴子:12,692,051
  • 3回复贴,共1

帮忙编译检查一下这个代码哪里错了

只看楼主收藏回复

class Link
{
         class Node
         {
                 private String name;
                 private Link next;
                 public Node(String name)
                 {
                         this.name=name;
                 }
                 public void addNode(Node newNode)
                 {
                         if(this.next==null)
                         {
                                 this.next=newNode;
                         }
                         else
                         {
                                 this.next.addNode(newNode);
                         }
                 }
                 public void printNode()
                 {
                         System.out.println(this.name);
                         if(this.next!=null)



IP属地:广东1楼2011-04-18 16:50回复
                             {
                                     this.next.printNode();
                             }
                     }
             }
            
             private Node root;
             public void add(String name)
             {
                     Node newNode=new Node(name);
                     if(root==null)
                     {
                             this.root=newNode;
                     }
                     else
                     {
                             this.root.addNode(newNode);
                     }
             }
             public void print()
             {
                     if(root!=null)
                     {
                             this.root.printNode();
                     }
             }
    }
    class Test
    {
             public static void main(String []args)
             {
                     Link l=new Link();
                     l.add("a");
                     l.add("b");
                     l.add("c");
                     l.add("d");
                     l.add("e");
                     l.add("f");
                     l.add("g");
                     l.print();
             }
    }


    IP属地:广东2楼2011-04-18 16:50
    回复
      private Link next;
      这句有问题,只要改为private Node next;就可以了


      3楼2011-04-18 20:57
      回复
        果然是这样,编译器报错真不是随便看得懂


        IP属地:广东4楼2011-04-18 22:30
        回复