当前位置:网站首页>Article 1 of linked list
Article 1 of linked list
2022-04-22 04:39:00 【Martin__ Liu】
The first part of the linked list
1. Introduce
- Lists are stored as nodes
- Each node includes data Domain , next Domain
- The nodes of the linked list are not necessarily stored continuously
- The linked list is divided into the linked list of the leading node and the linked list of the non leading node , Determine according to the actual situation
1.1 Head node
- Nodes that do not store specific data
- The function is to represent the head of the single linked list
- next The label of the domain pointing to the next node ( Next node address )
1.2 temp = temp.next An understanding of
public void add(HeroNode heroNode){
// because head Don't move . We need an auxiliary traversal temp
HeroNode temp = head;
// Traversing the linked list , Find the last node
while(true){
// If the last node is not found , take temp Move one back
if(temp.next == null){
break;
}
// If not found , Give Way temp Move one bit back
temp = temp.next;// The of the current last node next Assign the value of to the current need
// Add the location of the element , As their memory address
}
// When to exit while In the cycle ,temp Point to the end of the linked list
temp.next = heroNode;
}
- In the linked list, only
headWhen , Add to ithero1When :- (
temp = head;) At this time temp Refers to head In itself temp.next == nullby true, immediate withdrawal while loop , take node1 Your storage address is given to temp
- (
- Adding
hero2When :- take head Assign a value to temp, find node1 node , Find out node1 Node next by null, At this time temp It means node1.
- take node1 Node next Point to node2 Storage location
And so on …
Common nodes in the single linked list access content :
- label , That is, the so-called address
- Data fields , The code here is number, nickname,name Three data
- next Domain , The number pointing to the next label
| The name of the node | Node address | Node data | Point to the node |
|---|---|---|---|
| head | null | null | next Domain =1 |
| hero1 | ------ | 1,“ Song Jiang ”," Timely rain " | ------ |
| hero2 | ------ | 2,“ Jun-yi lu ”," Yu Qilin " | ------ |
| hero3 | ------ | 3,“ Wu Yong ”," A wise man " | ------ |
| hero4 | ------ | 4,“ Lin Chong ”," Leopard head " | ------ |
It's important to be careful here , The addresses of nodes are randomly assigned . But once it's determined, it's fixed .
Code :
package Package01;
import com.sun.xml.internal.bind.v2.runtime.unmarshaller.XsiNilLoader;
public class SingleLinkedListDemo {
public static void main(String[] args) {
// Conduct a test
// Create the node first
HeroNode hero1 = new HeroNode(1," Song Jiang "," Timely rain ");
HeroNode hero2 = new HeroNode(2," Jun-yi lu "," Yu Qilin ");
HeroNode hero3 = new HeroNode(3," Wu Yong "," A wise man ");
HeroNode hero4 = new HeroNode(4," Lin Chong "," Leopard head ");
// Create a linked list
SingleLinkedList singleLinkedList = new SingleLinkedList();
// Join in
singleLinkedList.add(hero1);
singleLinkedList.add(hero2);
singleLinkedList.add(hero3);
singleLinkedList.add(hero4);
// Show me a
singleLinkedList.list();
}
}
// Definition SingleLinkedList To manage heroes
class SingleLinkedList{
// Initialize a header node first , Don't move the head node , Do not store specific data
private HeroNode head = new HeroNode(0,"","");
// Add nodes to one-way linked list
// Ideas , When the numbering sequence is not considered
//1. Find the last node of the current linked list
//2. The last node's next Just point to the new node
public void add(HeroNode heroNode){
// because head Don't move . We need an auxiliary traversal temp
HeroNode temp = head;
// Traversing the linked list , Find the last node
while(true){
// If the last node is not found , take temp Move one back
if(temp.next == null){
break;
}
// If not found , Give Way temp Move one bit back
temp = temp.next;// The of the current last node next Assign the value of to the current need
// Add the location of the element , As their memory address
}
// When to exit while In the cycle ,temp Point to the end of the linked list
temp.next = heroNode;
}
// Show list , Traverse
public void list(){
// Need an auxiliary variable
// First judge whether the linked list is empty
if(head.next == null){
System.out.println(" Linked list is empty. ");
return;
}
// The head node cannot move , So we need an auxiliary variable to traverse
HeroNode temp = head.next;
while(true){
// Judge whether it is at the end of the linked list
if(temp == null){
break;
}
// If it's not empty , Just output the information of the node
System.out.println(temp);
//temp Move backward , Be sure to move back
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 + "]";
}
}
Output :
"C:\Program Files\Java\jdk1.8.0_152\bin\java.exe" "-javaagent:D:\ Software download \idea\idea\IntelliJ IDEA 2021.1\lib\idea_rt.jar=53370:D:\ Software download \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:\ Liu Jiancheng \java\project\HSP\out\production\Modulate03" Package01.SingleLinkedListDemo
HeroNode [no=1,name= Song Jiang ,nickname= Timely rain ]
HeroNode [no=2,name= Jun-yi lu ,nickname= Yu Qilin ]
HeroNode [no=3,name= Wu Yong ,nickname= A wise man ]
HeroNode [no=4,name= Lin Chong ,nickname= Leopard head ]
Process finished with exit code 0
版权声明
本文为[Martin__ Liu]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220430584361.html
边栏推荐
猜你喜欢

CommDGI: Community detection oriented deep graph infomax 2020 CIKM

DNS domain name system - directory service of the Internet

树莓派4B编译paddlelite(详细步骤2022年)

How expensive is the "salary" of software testing industry?

权威机构统计:2021 年最佳数据中心网络公司,中国华为和H3C上榜

7_ Data analysis - Evaluation

Reenter leetcode (392. Judgment subsequence)

2022年山东省安全员C证考试题及在线模拟考试

MUI-弹出菜单
![[concurrent programming 045] what is pseudo shared memory sequence conflict? How to avoid?](/img/27/c0cc4a6ae809fc662277dfe5629b01.png)
[concurrent programming 045] what is pseudo shared memory sequence conflict? How to avoid?
随机推荐
Unity的随机数
Introduction to C - parallel programming
爬演员名字加链接
[part of speech] 23. Modal verb 2 [have to] [out to] [dare] [be able to] [need n't]
What are the main aspects of mobile app testing?
目标检测--轻量级网络(截至2022-04-21)
NVIDIA, Wanxiang, fengyuzhu, dream chaser Fund... Talk about "new opportunities for yuan universe and industry" - 2022 yuan universe cloud Summit
Keras深度学习实战(2)——使用Keras构建神经网络
[experience] Why does the IP address of HP printer start with 169.254
AT32 MCU F435/437 DFU DEMO
Financial retail map - transaction flow warning map
一文告诉你分析即服务(AaaS)到底是什么
Solution to Chinese translation of GoLand (in case of failure to download plug-ins)
AT32 MCU F435/437 DFU DEMO
H7-tool releases firmware v2 15. For offline recording, the full series SPI flash of Renesas, Hetai and is25wp are added (2022-04-14)
crypto-js加密算法库【安装教程、缓存加密】
仿真生成随机数计算生成每个同学生日
How much do you know about the testing methods of software testing?
2022G2电站锅炉司炉考试练习题及在线模拟考试
13.bufferevent接受和发送数据