2016-02-12 135 views
-2
下面

是数据结构功课官方Java代码节点添加到一个链表,并让我们创造的是LinkedList的自己......:如何将节点添加到链接列表以及如何自己创建链接列表?

class Node { 
    double data; 
    Node next; 
    Node(double data) { 
     this.data = data; 
    }  
} 

Node node = new Node(23.334); 
class SortedLinkedList { 
    Node head; 
    public void insert(Node node) { 
     if(head == null){ 
      head = node; 
     } 
     else if (node.data < head.data) { 
      head.data=node.data; 
      return; 
     } else { 
      Node current = head; 
      // find where to insert it: 
      while (current.next != null) { 
       if (node.data < current.data) { 
        node.data = current.next; 
        // insert it! 
        // NEED SOME CODE 
        return; 
       } 
      } 
      previous=current; 
      current=current.next; 
      // You got here and didn't insert! 
      // insert it here 
      // NEED SOME CODE 
     } 
    } 

} 

我错过了什么,使其工作? 另外,我们如何将节点添加到链表? 我试图

SortedLinkedList sll =new SortedLinkedList();

但是系统说

Error: | cannot find symbol | symbol: class SortedLinkedList | SortedLinkedList sll =new SortedLinkedList(); | ^--------------^

,当我尝试添加节点

sll.insert(new Node (43.221);

系统总是说

Incomplete Java expression. Restarting and clearing everything...

什么是错的,如何解决他在我的代码问题请?以及如何创建链接列表?

+0

'Node node = new Node(23.334);' 此代码不在方法或类声明中。删除它,它应该编译。 – MartinS

+0

似乎像我的功课... –

+0

@MartinS只需删除它?那么如何在这个节点的数目旁创建一个链表呢? –

回答

0

好,无论是:

答:你试图执行从其他地方具有main方法,在这种情况下,Node node = new Node(23.334);行是格格不入的,应予删除该程序。另外,您应该在示例中添加main方法。 B:您试图在没有main方法的情况下执行此代码。请添加main方法。 或...

C:所有这些代码都在main方法中。请将Node node = new Node(23.334);以外的所有代码移出main方法。

+0

后一点 - 为什么?如果所有的代码都在'main'方法中,那就没问题了。 –

+0

@BoristheSpider它看起来很糟糕。 – AndrewIsOffline

+0

但它会_compile_ - 它目前没有。所以这可能不是问题。 –