关于Java单链表的疑惑

Click_Me 2009-08-28 06:10:18
首先代码如下:

package com.dbstructor.oop2;


class Link {
public int iData;
public double dData;
public Link next;

public Link(int id, double dd){
iData = id;
dData = id;
}

public void displayLink(){
System.out.print("{" + iData + "," + dData + "}");
}
}
class LinkList {
private Link first;
public LinkList(){
first = null;
}

public boolean isEmpty(){
return (first == null);
}

// 插入链接点
public void insertFirst(int id, double dd){
Link newLink = new Link(id, dd);
newLink.next = first;
first = newLink;
}

// 删除链接点
public Link deleteFirst(){
Link temp = first;
first = first.next;
return temp;
}

public void displayList(){
System.out.print("List (first-->last): ");
Link current = first;
while(current != null){
current.displayLink();
current = current.next;
}
System.out.println("");
}
}

public class LinkListApp {

public static void main(String[] args) {
LinkList theList = new LinkList();
theList.insertFirst(22, 2.99);
theList.insertFirst(44, 4.99);
theList.insertFirst(66, 6.99);
theList.insertFirst(88, 8.99);

theList.displayList();

while(!theList.isEmpty()){
Link aLink = theList.deleteFirst();
System.out.print("Deleted ");
aLink.displayLink();
System.out.println("");
}

theList.displayList();

}

}

...全文
270 29 打赏 收藏 转发到动态 举报
写回复
用AI写文章
29 条回复
切换为时间正序
请发表友善的回复…
发表回复
echomad 2012-08-01
  • 打赏
  • 举报
回复
delete()还应该加个非空判断吧
jia7007 2009-08-29
  • 打赏
  • 举报
回复
学习了,正用学呢
Click_Me 2009-08-28
  • 打赏
  • 举报
回复

回楼上的兄弟,我这个就是单链表 所以一个入口而已,以后会渐渐扩展的.所以会经常来问大家的
因为我是自学JAVA.谢谢大家.都很热心...
bigbug9002 2009-08-28
  • 打赏
  • 举报
回复
[Quote=引用 24 楼 click_me 的回复:]
Java code
谢谢楼上的兄弟们.
LinkList theList=new LinkList();
theList.insertFirst(22,2.99);
这个我这样理解大伙看对不对 对了就表示通了呵呵..

首先LinkList theList=new LinkList();实例化对象即首链接点.并初始了Link类型的 first为Null
其次theList.insertFirst(¡­
[/Quote]
就是那个样子,

你的添加结点和删除结点都在表头,也就是first,那里,所以我说你的链表和栈一样,是一种先入后出的结构。
yanliang_xt 2009-08-28
  • 打赏
  • 举报
回复
正解。
Click_Me 2009-08-28
  • 打赏
  • 举报
回复

谢谢楼上的兄弟们.
LinkList theList = new LinkList();
theList.insertFirst(22, 2.99);
这个我这样理解大伙看对不对 对了就表示通了呵呵..

首先LinkList theList = new LinkList();实例化对象即首链接点.并初始了Link类型的 first为Null
其次theList.insertFirst(22, 2.99);在这个方法里
(1)Link newLink = new Link(id, dd);也是新建一个newLink链接点,并初始化Link类型的next为Null
(2)newLink.next = first;将first指针地址覆盖next的,即next指向了first的引用
(3)first = newLink;将first指向了newLink的新引用
最终链结构就是first--->newLink(next)---->Null

大伙指正下 准备结贴






yanliang_xt 2009-08-28
  • 打赏
  • 举报
回复
大虫精神可嘉
yanliang_xt 2009-08-28
  • 打赏
  • 举报
回复
其实都是结点,只是结点的作用不同罢了。
有的是为了存储数据
有的是为了指引的作用
bigbug9002 2009-08-28
  • 打赏
  • 举报
回复


删除结点:
1.用一个引用指(temp)向first指向的结点。
2.让first指向first指向的结点的next属性指向的结点。
3.返回temkp.
knightzhuwei 2009-08-28
  • 打赏
  • 举报
回复
每一块叫结点 first和next是引用
Click_Me 2009-08-28
  • 打赏
  • 举报
回复
嗯 谢谢楼上几位
再有就是我#6的图
每一块叫一个链接点么? 还是其实的first和next叫链接点
bigbug9002 2009-08-28
  • 打赏
  • 举报
回复


第一步,新建结点,并用newLink指向新结点。
第二步,新结点的next指向first指向的结点。
第三步,first指向新结点。
knightzhuwei 2009-08-28
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 click_me 的回复:]
newLink.next = first;
这句话 到底是next指向了first的引用 还是first指向了next的引用呢?
[/Quote]
next指向了原来first指向的那个节点
yanliang_xt 2009-08-28
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 click_me 的回复:]
newLink.next = first;
这句话 到底是next指向了first的引用 还是first指向了next的引用呢?
[/Quote]

新的结点的next指向了first指向的内容。
bigbug9002 2009-08-28
  • 打赏
  • 举报
回复
public void insertFirst(int id, double dd){
Link newLink = new Link(id, dd); //新建一个结点.
newLink.next = first; //新建的结点的next指向first指向的结点。
first = newLink; //first指向新结点。

}

Click_Me 2009-08-28
  • 打赏
  • 举报
回复
newLink.next = first;
这句话 到底是next指向了first的引用 还是first指向了next的引用呢?
yanliang_xt 2009-08-28
  • 打赏
  • 举报
回复
删除操作它也是从起始位置进行删除的:
Link temp = first;
first = first.next;
return temp;
直接把first指向第二次结点就可以了,也就是第一个结点所指向的下一个 first = first.next
yanliang_xt 2009-08-28
  • 打赏
  • 举报
回复
每个链表都有一个first
它也是一个结点类型的。
当然我们也可以把它理解为整个链表的一个指针。
它标识了链表的存在。
在插入操作中:
因为都是在起始位置上作插入操作的。所以first总是会指向新的结点,当然要使新的结点插入到整个链表中还要做新的结点的next指向原来first的所指向的那个结点,
每插入一点新的结点都有这样的操作。这样链表就不断的增长。

knightzhuwei 2009-08-28
  • 打赏
  • 举报
回复
图片是静态的 又不可能看出三步来 图片里的那个33就是新new的节点
knightzhuwei 2009-08-28
  • 打赏
  • 举报
回复
链表的一个节点 包含一个值和一个指向下个节点的引用 一个链表首先有个first指向第一个节点 你要insertFirst的时候 首先就要先new一个节点:
Link newLink = new Link(id, dd)
然后 把这个新new节点所持有的引用指向原来的第一个节点:
newLink.next = first;
然后把first这个引用指向新new的节点
first = newLink;
就是这样的
加载更多回复(9)

62,616

社区成员

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

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