当前位置:网站首页>定义链表(链表)
定义链表(链表)
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
边栏推荐
- Realize data value through streaming data integration (3) - real-time continuous data collection
- Jerry's factors that usually affect CPU performance test results are: [article]
- LeetCode-608. Tree node
- Solve the problem of installing VMware after uninstalling
- Prefix sum of integral function -- Du Jiao sieve
- 杰理之通常影响CPU性能测试结果的因素有:【篇】
- Odoo 服务器搭建备忘
- Sim Api User Guide(5)
- 通过流式数据集成实现数据价值(5)- 流分析
- shell脚本免交互
猜你喜欢
随机推荐
DBA常用SQL语句 (5) - Latch 相关
第120章 SQL函数 ROUND
[untitled]
Juc并发编程06——深入剖析队列同步器AQS源码
杰理之有时候定位到对应地址的函数不准确怎么办?【篇】
Sim Api User Guide(7)
The central control learning infrared remote control module supports network and serial port control
1、两数之和(哈希表)
Go语言实践模式 - 函数选项模式(Functional Options Pattern)
Classic routine: DP problem of a kind of string counting
2022年流动式起重机司机考试题库模拟考试平台操作
DBA常用SQL语句(6)- 日常管理
通过流式数据集成实现数据价值(2)
Pyqt5 and communication
基于PyQt5实现弹出任务进度条功能示例
Understand the new economic model of platofarm and its ecological progress
Yarn核心参数配置
Windows安装redis并将redis设置成服务开机自启
142、环形链表||
Pyqt5与通信