publicclassLinkList{protectedNodefirstNode;publicvoidInit(){firstNode=null;}protectedvoidAdd(NodenewNode){if(firstNode==null){firstNode=newNode;firstNode.NextNode=firstNo...
public class LinkList {
protected Node firstNode;
public void Init(){
firstNode = null;
}
protected void Add(Node newNode){
if (firstNode == null){
firstNode = newNode;
firstNode.NextNode = firstNode;
return;
}
Node currentNode = firstNode;
Node preNode = null;
while (currentNode.NextNode != firstNode){
preNode = currentNode;
currentNode = currentNode.NextNode;
}
currentNode.NextNode = newNode;
newNode.NextNode = firstNode;
}
}
class Node{
public Node(int dataValue){
Data = dataValue;
NextNode = null;
}
public Node(){
NextNode = null;
}
public int Data;
public Node NextNode;
public void ShowData(){
System.out.println(Data);
}
}
这是数据结构中一个线性表链式存储结构的Java代码,向表中插入一个newNode,我想问一下,这个newNode是插入到哪里的,是首节点还是表中任意一个节点。顺便把Add方法中的每行代码解释一下,还有后面一个Node类的作用。
展开