当前位置:网站首页>定义链表(链表)
定义链表(链表)
2022-04-23 10:12:00 【Popuessing's Jersey】
定义int型链表
public class ListNode {
int val; //数据:节点数据
ListNode next; //对象:引用下一个节点对象,
//添加构造方法
ListNode(int val){//构造方法:构造方法和类名相同
this.val = val; //把接收到的参数赋值给当前类的val变量
}
}
定义泛型链表
public class ListNode<E> {
E val; //数据:节点数据
ListNode next; //对象:引用下一个节点对象,
//添加构造方法
ListNode(E val){//构造方法:构造方法和类名相同
this.val = val; //把接收到的参数赋值给当前类的val变量
}
}
创建链表以及遍历链表
static class Test_{
//静态声明在内部类中是不允许的
public static void main(String[] args) {
//创建首节点
ListNode nodeSta = new ListNode(0);
//声明一个变量用来在移动过程中指向当前节点
ListNode nextNode;
//指向首节点
nextNode = nodeSta;
//创建链表(长度为10)
for (int i = 1; i <10 ; i++) {
//生成新节点
ListNode node = new ListNode(i);
//连接新节点
nextNode.next = node;
//把当前节点往后移动
nextNode = nextNode.next;
}//当循环完毕后,nextNode指向最后一个节点
//重新赋值让移动节点指向首节点
nextNode = nodeSta;
//打印输出
print(nextNode);
}
static void print(ListNode listNode){
//创建链表节点
while (listNode!=null){
if(listNode.next==null){
System.out.print(listNode.val);
}else {
System.out.print(listNode.val + "->");
}
listNode = listNode.next;
}
}
}
输出结果:0->1->2->3->4->5->6->7->8->9
插入链表
//插入节点
while(nextNode!=null){
//这里因为之前使用的示范型定义的链表,因此需要规定val的数据类型为int
if((int)nextNode.val==5){
ListNode newnode = new ListNode(99);
//保存下一个节点
ListNode node = nextNode.next;
//插入新节点
nextNode.next = newnode;
//新节点的下一个节点指向之前保存的节点
newnode.next = node;
}
nextNode=nextNode.next;
}//循环之后,nextNode指向第一个节点
nextNode =nodeSta;
print(nextNode);
}
//打印输出
static void print(ListNode listNode){
//创建链表节点
while (listNode!=null){
if(listNode.next==null){
System.out.print(listNode.val);
}else {
System.out.print(listNode.val + "->");
}
listNode = listNode.next;
}
}
输出结果: 0->1->2->3->4->5->99->6->7->8->9
替换节点
//替换节点
while(nextNode!=null){
if ((int)nextNode.val==4){
//生成新的节点
ListNode newnode = new ListNode(98);
//保存替换节点的下一个节点
ListNode node = nextNode.next.next;
//将替换节点指向null,等待垃圾回收
nextNode.next.next = null;
//将当前节点指向新节点
nextNode.next = newnode;
//将节点指向原来的替换节点的下一个节点
newnode.next = node;
}
//移动当前节点
nextNode = nextNode.next;
}
输出结果:0->1->2->3->4->98->99->6->7->8->9
删除节点
//删除节点
while(nextNode!=null){
if ((int)nextNode.val==3){
//保留当前节点的下一个节点的指向节点
ListNode node = nextNode.next.next;
//将当前节点的下一个节点指向null,等待垃圾回收
nextNode.next.next = null;
//将当前节点指向保留的节点
nextNode.next = node;
}
//移动节点向后移动
nextNode = nextNode.next;
}
输出结果:0->1->2->3->98->99->6->7->8->9
版权声明
本文为[Popuessing's Jersey]所创,转载请带上原文链接,感谢
https://blog.csdn.net/CoCo629vanilla/article/details/121386473
边栏推荐
猜你喜欢
MapReduce压缩
Planning and construction of industrial meta universe platform
正大国际讲解道琼斯工业指数到底是什么?
lnmp的配置
2022 mobile crane driver test question bank simulation test platform operation
Exercise questions and simulation test of refrigeration and air conditioning equipment operation test in 2022
failureForwardUrl与failureUrl
Comparative analysis of meta universe from the dimension of knowledge dissemination
0704、ansible----01
Epidemic prevention registration applet
随机推荐
DBA常用SQL语句(3)- cache、undo、索引和等待事件
Jerry's factors that usually affect CPU performance test results are: [article]
Configuration of LNMP
杰理之有时候定位到对应地址的函数不准确怎么办?【篇】
一文看懂 LSTM(Long Short-Term Memory)
通过流式数据集成实现数据价值(5)- 流处理
19、删除链表的倒数第N个节点(链表)
JUC concurrent programming 07 -- is fair lock really fair (source code analysis)
杰理之通常程序异常情况有哪些?【篇】
Epidemic prevention registration applet
"Gu Yu series" airdrop
JVM——》常用命令
ARM调试(1):两种在keil中实现printf重定向到串口的方法
Common DBA SQL statements (4) - Top SQL
Windows安装redis并将redis设置成服务开机自启
正大国际讲解道琼斯工业指数到底是什么?
杰理之AES能256bit吗【篇】
Prefix sum of integral function -- Du Jiao sieve
Sim Api User Guide(4)
Examination questions and answers of the third batch (main person in charge) of Guangdong safety officer a certificate in 2022