本文实例为大家分享了java实现简单单链表的具体代码,供大家参考,具体内容如下
一、定义:
单链表是一种链式存取的数据结构,用一组地址任意的存储单元存放线性表中的数据元素。链表中的数据是以结点来表示的,每个结点的构成:元素(数据元素的映象) + 指针(相当于JAVA中的引用,指示后继元素存储位置,),元素就是存储数据的存储单元,指针就是连接每个结点的地址数据。
二、结构:
如图所示,data就是当前节点的数据,next是指针,指针存放的是内存地址,是当前结点的下一结点内存地址,顺着这个地址就能找到下一个结点。
三、代码实现:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
package com.example.demo.linkedlist;
/**
* 结点
* Created by xinan on 2021/02/23
*/
public class Node {
public Integer value;
public Node next;
public Node(Integer value) {
this .value = value;
}
public Node(Integer value, Node next) {
this .value = value;
this .next = next;
}
public Integer getValue() {
return value;
}
public void setValue(Integer value) {
this .value = value;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this .next = next;
}
}
|
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
package com.example.demo.linkedlist;
/**
* 单链表
* Created by xinan on 2021/2/23
*/
public class SingleLinkedList {
public Node head;
/**
* 从头部添加
* @param data 待添加数据
*/
public void addHead(Integer data) {
Node node = new Node(data);
node.next = head;
head = node;
}
/**
* 从尾部添加
* @param data 待添加数据
*/
public void addLast(Integer data) {
Node node = new Node(data);
if (head == null ) {
head = node;
return ;
}
Node temp = head;
while (temp.next != null ) {
temp = temp.next;
}
temp.next = node;
}
/**
* 获取链表的长度
* @return 链表长度
*/
public Integer length() {
int length = 0 ;
Node temp = head;
while (temp != null ) {
temp = temp.next;
length ++;
}
return length;
}
/**
* 从指定下标处添加
* @param index 指定下标
* @param data 待添加的数据
*/
public void addByIndex( int index, Integer data) {
if (index < 0 || index > length()) {
System.out.println( "插入下标不合规,请检查!" );
return ;
}
if (index == 0 ) {
addHead(data);
return ;
}
Node node = new Node(data);
Node temp = head;
for ( int i = 1 ; i < index; i++) {
temp = temp.next;
}
node.next = temp.next;
temp.next = node;
}
/**
* 指定下标删除
* @param index 指定下标
*/
public void deleteByIndex( int index) {
if (index < 0 || index > length()) {
System.out.println( "删除下标不合规,请检查!" );
return ;
}
if (index == 0 ) {
head = head.next;
return ;
}
Node temp = head;
for ( int i = 1 ; i < index; i++) {
temp = temp.next;
}
temp.next = temp.next.next;
}
/**
* 通过下标获取结点
* @param index 下标
* @return 结点
*/
public Node getByIndex(Integer index) {
if (index < 0 || index > length() - 1 ) {
System.out.println( "不存在此下标结点" );
}
Node temp = head;
int i = 0 ;
while (temp != null ) {
if (i == index) {
return temp;
}
i ++;
temp = temp.next;
}
return null ;
}
/**
* 打印链表值
*/
public void printLink() {
Node temp = head;
while (temp != null ) {
System.out.println(temp.value);
temp = temp.next;
}
}
/**
* 打印某个节点之后的所有值
* @param node
*/
public static void printAfterNode(Node node) {
while (node != null ) {
System.out.println(node.value);
node = node.next;
}
}
/**
* 清除单链表
*/
public void clearLink() {
head = null ;
}
/**
* 单链表反转
* @param head 头节点
*/
public Node reverseLink(Node head) {
Node prev = null ;
Node curr = head;
while (curr != null ) {
Node nextTemp = curr.next;
curr.next = prev;
prev = curr;
curr = nextTemp;
}
return prev;
}
/**
* 测试
* @param args
*/
public static void main(String[] args) {
SingleLinkedList linkNode = new SingleLinkedList();
linkNode.addHead( 2 );
linkNode.addHead( 3 );
linkNode.addHead( 5 );
linkNode.addLast( 9 );
linkNode.addLast( 7 );
System.out.println( "打印单链表: " );
linkNode.printLink();
Node byIndex1 = linkNode.getByIndex( 0 );
System.out.println( "获取下标为1的结点值: " + byIndex1.value);
linkNode.addByIndex( 2 , 8 );
System.out.println( "下标2添加后打印单链表: " );
linkNode.printLink();
linkNode.addByIndex( 0 , 11 );
System.out.println( "下标0添加后打印单链表: " );
linkNode.printLink();
linkNode.deleteByIndex( 0 );
System.out.println( "下标0删除后打印单链表: " );
linkNode.printLink();
Node node = linkNode.reverseLink(linkNode.head);
System.out.println( "反转后打印单链表: " );
printAfterNode(node);
}
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。
原文链接:https://blog.csdn.net/pavel101/article/details/114004379
相关文章
猜你喜欢
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-29 14
-
2025-05-27 40
-
2025-06-04 105
-
2025-05-29 46
-
2025-06-04 38
热门评论