小弟不解,求助!!!

我需要更加努力 2011-12-05 11:04:02

class Link{
public long dData;
public Link next;

public Link(long dd){
dData=dd;
}
//、、、、、、、、、、、、、、、


public void insert(long key){
Link newLink=new Link(key);
Link previous=null;
Link current =first;//这

if(previous==null)
first=newLink;//这
else
previous.next=newLink;
newLink.next=current;//这一点不明白什么意思,此时current的值是什么?null么?还有这句代码的意思
//是将指针指向本身么?(上面做标记的代码使我的理解是:newLink.next=newLink,但是我感觉不对啊)
}

...全文
103 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
lxbccsu 2011-12-06
  • 打赏
  • 举报
回复
[Quote=引用楼主 tao0329 的回复:]
Java code


class Link{
public long dData;
public Link next;

public Link(long dd){
dData=dd;
}
//、、、、、、、、、、、、、、、


public void insert(long key){
Link ……
[/Quote]
这就是一个swap;
楼主可以先学习下链表的一些基础知识;
insert表示要插入一个新元素到link中;

Link newLink=new Link(key);//新的结点
Link current =first;//取出current结点
if(previous==null)//没有前置结点
first=newLink;//把新结点放入link的第一个位置
else//有前置结点
previous.next=newLink;//把前置结点的下一个结点引用指向新的结点
newLink.next=current;把新结点的下一个结点引用指向上面获取的current结点

如果有图,就很清晰了,可以找个图理解下
龙腾冰 2011-12-06
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 xiongyu2006 的回复:]

一练习题,仅供楼主参考。
Java code

package com.study.pratice03;

class Link
{
private Node root;

class Node
{
private Node next;
private String name;

public Node(String s……
[/Quote]

怎么和我的代码这么象?
xiongyu2006 2011-12-06
  • 打赏
  • 举报
回复
一练习题,仅供楼主参考。

package com.study.pratice03;

class Link
{
private Node root;

class Node
{
private Node next;
private String name;

public Node(String str)
{
this.name = str;
}

public String getName()
{
return this.name;
}

public void addNode(Node node)
{
if (this.next == null)
{
this.next = node;
}
else
{
this.next.addNode(node);
}
}

public void printNode()
{
if (this.next == null)
{
System.out.print(this.getName());
}
else
{
System.out.print(this.getName() + "->");
this.next.printNode();
}
}

public boolean searchNode(String name)
{
if (this.getName().equals(name))
{
return true;
}
else
{
if (this.next == null)
{
return false;
}
else
{
return this.next.searchNode(name);
}
}
}

public void deleteNode(Node preNode, String name)
{
if (this.getName().equals(name))
{
preNode.next = this.next;
}
else
{
this.next.deleteNode(this, name);
}
}
}

public void add(String name)
{
Node newNode = new Node(name);
if (root == null)
{
this.root = newNode;
}
else
{
this.root.addNode(newNode);
}
}

public void printLink()
{
if (root != null)
{
this.root.printNode();
}
}

public boolean search(String name)
{
if (this.root.getName().equals(name))
{
return true;
}
else
{
return this.root.next.searchNode(name);
}
}

public void delete(String name)
{
if (this.search(name))// 判断节点是否存在
{
if (this.root.getName().equals(name))// 判断要删除的节点是否是头结点
{
if (this.root.next == null)// 判断头节点是否有后续节点
{
this.root = null;
}
else
{
this.root = root.next;// 更改头结点
}
}
else
{
this.root.next.deleteNode(root, name);
}
}
else
{
System.out.println("要删除的节点不存在!");
}
}
}

public class LinkedNode
{
public static void main(String[] args)
{
// TODO Auto-generated method stub
Link l = new Link();
l.add("根节点");
l.add("第一个节点");
l.add("第二个节点");
l.add("第三个节点");
l.add("第四个节点");
l.add("第五个节点\n");

l.printLink();
System.out.println(l.search("根节点"));
l.delete("根节点");
l.printLink();
l.delete("第八个节点");
l.delete("第四个节点");
l.printLink();

}

}

龙腾冰 2011-12-06
  • 打赏
  • 举报
回复
自己写链表的时候最好用上内部类这样会比较简单
lxbccsu 2011-12-06
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 tao0329 的回复:]
谁能解释一下我问到的问题么?谢谢
[/Quote]
上面之所以先需要Link current = first, 赋职给current,就是第二步first = newLink,此时first会引用newLink,防止丢失原来的结点,此时first和newLink是同一个对象;
第三步newLink.next = current就很清晰了,把newLink的next结点指向原来的first结点;这里其实可以写成first.next = current,因为此时first和newLink是同一个对象;


你完全可以拿出笔来画画(假设最初first == null):
setp1: insert(1)
Link current = first; ==> first==null, current == null

if(previous==null)
first = newLink; ==> first == 1, newLink == 1

newLink.next = current; ==> newLink.next == null

====================================================================
setp2: insert(2)
Link current = first; ==> first == 1, current == 1

if(previous==null)
first = newLink; ==> first == 2, newLink == 2

newLink.next = current; ==> newLink.next == 1


====================================================================
setp3: insert(3)
Link current = first; ==> first==2, current == 2

if(previous==null)
first = newLink; ==> first == 3, newLink == 3

newLink.next = current; ==> newLink.next == 2

..........
  • 打赏
  • 举报
回复
谁能解释一下我问到的问题么?谢谢

62,614

社区成员

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

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