【发布时间】:2011-06-25 19:32:24
【问题描述】:
我正在使用 Java 中的链表工作,所以我试图掌握单链表的概念。
head -> 12 -> 34 -> 56 -> null
head.next 将是 12(也与 node1 相同)。但是,什么是头呢?
更新:引用和指针有什么区别?
Update2:所以如果head 是12 并且head.next 是34,那么并不意味着下面的函数会跳过第一个节点来查看它是否为空?
public void add(Object data, int index)
// post: inserts the specified element at the specified position in this list.
{
Node temp = new Node(data);
Node current = head;
// crawl to the requested index or the last element in the list,
// whichever comes first
for(int i = 1; i < index && current.getNext() != null; i++)
{
current = current.getNext();
}
// set the new node's next-node reference to this node's next-node reference
temp.setNext(current.getNext());
// now set this node's next-node reference to the new node
current.setNext(temp);
listCount++;// increment the number of elements variable
}
来源:http://www.mycstutorials.com/articles/data_structures/linkedlists
【问题讨论】:
-
现在尝试断章取义地阅读问题。这没有任何意义:)
-
一些上下文可能会有所帮助...
标签: java data-structures linked-list