是数据结构功课官方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...
什么是错的,如何解决他在我的代码问题请?以及如何创建链接列表?
'Node node = new Node(23.334);' 此代码不在方法或类声明中。删除它,它应该编译。 – MartinS
似乎像我的功课... –
@MartinS只需删除它?那么如何在这个节点的数目旁创建一个链表呢? –