Java实现单链表的操作,求大神解答!!!

yuzeng222 2017-04-26 10:09:56
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();”这条语句是否也可以得到下一个节点?
如果不可以,请问这是为什么?
如果可以,请问这又是为什么?
求大神解答,谢谢!!!
...全文
258 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhujinqiang 2017-04-26
  • 打赏
  • 举报
回复
然后public void setNextContent(Node nextContent) 也变成public void setNextContent(Node Content)?? 因为你public Node content;//指针域是这个content 很绕人啊 有意义吗?
LS1firesoar 2017-04-26
  • 打赏
  • 举报
回复
毫无意义的问题
yuzeng222 2017-04-26
  • 打赏
  • 举报
回复
引用 2 楼 feiyudiaoling 的回复:
变量名只是个名称,没有实际的意义,而你这个指针域和数据域不是说你注释里换一下他们就真换了,而是说你在需要指针域的地方使用的是哪个变量,在需要数据域的地方使用的是哪个变量,
我现在就想声明成如下: public Node content;//指针域 public Node nextContent;//数据域 然后用“temp = temp.getContent();”这条语句得到下一个节点,按你的说法是应该可以的,请问这是为什么呀?这两个节点间的关系是什么呀?
青木刀风 2017-04-26
  • 打赏
  • 举报
回复
变量名只是个名称,没有实际的意义,而你这个指针域和数据域不是说你注释里换一下他们就真换了,而是说你在需要指针域的地方使用的是哪个变量,在需要数据域的地方使用的是哪个变量,

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧