1.介绍
- 列表是以节点的方式进行存储的
- 每一个节点包括data域, next域
- 链表的各个节点不一定是连续存放的
- 链表分为带头节点的链表和不带头结点的链表,根据实际情况来确定
1.1头节点
- 不存放具体数据的节点
- 作用就是表示的单链表的头
- next域指向下一个节点的标号(下一个节点地址)
1.2 temp = temp.next 的一个理解
public void add(HeroNode heroNode){
HeroNode temp = head;
while(true){
if(temp.next == null){
break;
}
temp = temp.next;
}
temp.next = heroNode;
}
- 在链表只有
head 的时候,往里面添加hero1 的时候:
- (
temp = head; ) 此时的temp指的head本身 temp.next == null 为true,直接退出while循环,将node1的存储地址给了temp - 在添加
hero2 的时候:
- 将head赋值给temp,找到node1节点,发现node1节点的next为null,此时的temp就是指的node1。
- 将node1节点的next指向node2的存储位置
以此类推…
单链表里面的普通的节点存取内容:
- 标号,也就是所谓的地址
- 数据域,这里的代码所存的是number, nickname,name三个数据
- next域,指向下一个标号的号码
节点名称 | 节点地址 | 节点数据 | 指向节点 |
---|
head | null | null | next域=1 | hero1 | ------ | 1,“宋江”,"及时雨" | ------ | hero2 | ------ | 2,“卢俊义”,"玉麒麟" | ------ | hero3 | ------ | 3,“吴用”,"智多星" | ------ | hero4 | ------ | 4,“林冲”,"豹子头" | ------ |
这里一定要注意,节点的地址是随机分配的。但是一旦确定下来就是固定的了。
代码:
package Package01;
import com.sun.xml.internal.bind.v2.runtime.unmarshaller.XsiNilLoader;
public class SingleLinkedListDemo {
public static void main(String[] args) {
HeroNode hero1 = new HeroNode(1,"宋江","及时雨");
HeroNode hero2 = new HeroNode(2,"卢俊义","玉麒麟");
HeroNode hero3 = new HeroNode(3,"吴用","智多星");
HeroNode hero4 = new HeroNode(4,"林冲","豹子头");
SingleLinkedList singleLinkedList = new SingleLinkedList();
singleLinkedList.add(hero1);
singleLinkedList.add(hero2);
singleLinkedList.add(hero3);
singleLinkedList.add(hero4);
singleLinkedList.list();
}
}
class SingleLinkedList{
private HeroNode head = new HeroNode(0,"","");
public void add(HeroNode heroNode){
HeroNode temp = head;
while(true){
if(temp.next == null){
break;
}
temp = temp.next;
}
temp.next = heroNode;
}
public void list(){
if(head.next == null){
System.out.println("链表为空");
return;
}
HeroNode temp = head.next;
while(true){
if(temp == null){
break;
}
System.out.println(temp);
temp = temp.next;
}
}
}
class HeroNode{
public int no;
public String name;
public String nickname;
public HeroNode next;
public HeroNode(int hNo,String hName,String hNickname){
this.no = hNo;
this.name = hName;
this.nickname = hNickname;
}
@Override
public String toString(){
return "HeroNode [no="+ no +",name=" + name + ",nickname=" + nickname + "]";
}
}
输出:
"C:\Program Files\Java\jdk1.8.0_152\bin\java.exe" "-javaagent:D:\软件下载\idea\idea\IntelliJ IDEA 2021.1\lib\idea_rt.jar=53370:D:\软件下载\idea\idea\IntelliJ IDEA 2021.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_152\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_152\jre\lib\rt.jar;E:\刘建成\java\project\HSP\out\production\Modulate03" Package01.SingleLinkedListDemo
HeroNode [no=1,name=宋江,nickname=及时雨]
HeroNode [no=2,name=卢俊义,nickname=玉麒麟]
HeroNode [no=3,name=吴用,nickname=智多星]
HeroNode [no=4,name=林冲,nickname=豹子头]
Process finished with exit code 0
|