在教科书“使用Java 6E进行数据结构和算法”中找到,此实现在尝试创建新节点时在addFirst和addLast方法中给出了错误。它说无法从SinglyLinkedList.Node转换为SinglyLinkedList.Node
这是代码
html
<tr>
<td><input type="hidden" data-val="true" data-val-required="The BlogID field is required." id="item_BlogID" name="item.BlogID" value="1" /></td>
<td><input type="hidden" data-val="true" data-val-required="The PostID field is required." id="item_PostID" name="item.PostID" value="1" /></td>
<td>
<input type="text" style="border:0;" readonly id="item_Url" name="item.Url" value="blog1@test.com" />
</td>
<td>
<input type="text" id="item_Title" name="item.Title" value="Title1" />
</td>
<td>
<input type="text" id="item_Content" name="item.Content" value="Content1" />
</td>
</tr>
答案 0 :(得分:0)
更新您的addFirst
和addLast
方法,如下所示:
public void addFirst(E e) {
head = new Node<E>(e, head); // update here
if(size == 0){
tail = head;
}
size++;
}
public void addLast(E e){
Node<E> newest = new Node<E>(e, null); // update here
if(isEmpty()){
head = newest;
}
else{
tail.setNext(newest);
}
tail = newest;
size++;
}
然后我就可以创建并使用它了。
public class A {
public static void main(String[] args) {
SinglyLinkedList list = new SinglyLinkedList<Integer>();
list.addFirst(new Integer(1));
list.addLast(new Integer(2));
System.out.println(list.first());
System.out.println(list.last());
}
}