Node类(节点类)
public class Node {
public Node content;//数据域
public Node nextContent;//指针域

public Node(Node content, Node nextContent) {
super();
this.content = content;
this.nextContent = nextContent;
} public Node() {
} public Node getContent() {
return content;
} public void setContent(Node content) {
this.content = content;
} public Node getNextContent() {
return nextContent;
} public void setNextContent(Node nextContent) {
this.nextContent = nextContent;
}
}LinkList类
public class LinkList {
public static Node top;//头节点
public static int count;//节点长度

public LinkList(){
top = null;
count = 0;
} public static void main(String[] args) {
// TODO Auto-generated method stub
Node node1 = new Node();
Node node2 = new Node();
Node node3 = new Node();
Node node4 = new Node();
Node nodes1 = new Node(node1,node2);
Node nodes2 = new Node(node2,node3);
Node nodes3 = new Node(node3,node4);
Node nodes4 = new Node(node4,null);

LinkList linkList = new LinkList();
linkList.addNode(nodes1);
linkList.addNode(nodes2);
linkList.addNode(nodes3);
linkList.addNode(nodes4);

linkList.print();
}

/**
 * 节点的添加
 * @param data
 */
@SuppressWarnings("null")
public void addNode(Node data){
Node node = new Node(data,null);
Node temp = null;

if(top != null){
temp = top;
while(temp.getNextContent() != null){
temp = temp.getNextContent();
}
temp.setNextContent(node);
} else {
top = node;
temp = node;
}

count ++;
}
/**
 * 打印
 */
public void print(){
Node temp = top;

while(temp != null){
System.out.println(temp.getContent().toString());
temp = temp.getNextContent();
}
}

} 问题如下:
我声明了两个节点类
public Node content;//数据域
public Node nextContent;//指针域
用“temp = temp.getNextContent();”这条语句可以得到下一个节点,
如果我声明成如下:
public Node content;//指针域
public Node nextContent;//数据域
如果换成“temp = temp.getContent();”这条语句是否也可以得到下一个节点?
如果不可以,请问这是为什么?
如果可以,请问这又是为什么?
求大神解答,谢谢!!!

解决方案 »

  1.   

    然后public void setNextContent(Node nextContent)
    也变成public void setNextContent(Node Content)??
    因为你public Node content;//指针域是这个content
    很绕人啊
    有意义吗?
      

  2.   


    我现在就想声明成如下:
    public Node content;//指针域
    public Node nextContent;//数据域
    然后用“temp = temp.getContent();”这条语句得到下一个节点,按你的说法是应该可以的,请问这是为什么呀?这两个节点间的关系是什么呀?