java实现单链表NodeList

article/2025/10/6 8:39:12

目录

一、单链表的概念

二、单链表的实现

1.定义节点类

2.定义单链表

1.属性

2.方法

三、完整实现


一、单链表的概念

单链表的概念:单链表是一种链式存取的数据结构,链表中的数据是以结点来表示的,每个结点的构成:元素(数据元素的映象) + 指针(指示后继元素存储位置),元素就是存储数据的存储单元,指针就是连接每个结点的地址数据。以“结点的序列”表示的线性表称作线性链表(单链表),单链表是链式存取的结构。

具体而言

单链表由头结点和元素结点连接组成。

单链表的每个结点由数据域指针域组成。数据域存储数据,指针域存储下一个结点的地址。

https://www.dotcpp.com/oj/ueditor/php/upload/image/20190618/1560862606257333.png

 

单链表的最后一个结点的指针域指向空(NULL)。

头结点的作用:

统一操作--由于开始结点的位置被存放在头结点的指针域中,所以在链表的第一个位置上的操作和其他位置的结点的操作一样,无需做其他特殊处理。
统一判空--链表为空时,头结点的指针域指向空,即可判空,链表不为空时,头结点的指针域指向第一个元素结点,头结点的指针域不为空,即可判不空。

 

二、单链表的实现

1.定义节点类

package com.zsj1105;public class Node<E> {public E value;   //存储结点的值public Node next;   //用来存储下一个结点的地址//空参构造方法public Node(){}//有参构造方法public Node(E value,Node next){//每个结点都有一个数据域和地址域this.value = value;this.next = next;}}

2.定义单链表

1.属性

* head 头部结点

* tail 尾部结点

* curr 当前结点

* size 元素个数

    //属性private Node<E> head;  //设置头部结点private Node<E> tail;  //设置尾部结点private Node<E> curr;  //设置当前结点private int size;  //设置元素个数

2.方法

(1)构造方法

* 1.NodeList() 空参数构造方法
* 2.NodeList(Node node) 由一个结点创建一个链表

 //构造方法/*** 无参数构造方法*/NodeList() {head = tail = curr = new Node();size = 0;}/*** 由一个结点创建链表** @param node*/NodeList(Node node) {head = tail = curr = new Node();this.size = 1;this.head.next = node;this.tail = node;}

(2)方法

* 1.int getSize() 获取总元素个数
* 2.Node getHead() 获取头部结点
* 3.Node getCurr() 获取当前结点
* 4.Node getTail() 获取尾部结点
* 5.void setCurr() 设置当前结点
* 6.void print() 打印链表中的所有元素
* 7.void addToHead(E value)在头部结点后面插入结点
* 8.void addToTail(E value)在尾部结点后面插入结点
* 9.void insert(E value)将当前结点值设为value
* 10.delete()删除当前结点之后的一个结点
* 11.delete(E value)删除第一个值为value的结点
* 12.find(E value)找到值为value的结点
* 13.replace(E value)将当前结点的值替换为value
* 14.replace(E value1, E value2)将值为value1的结点的值替换为value2

  //方法/*** 获取元素个数** @return*/public int getSize() {return size;}/*** 获取头部结点** @return*/public Node<E> getHead() {return head;}/*** 获取当前结点** @return*/public Node<E> getCurr() {return curr;}/*** 获取尾部结点** @return*/public Node<E> getTail() {return tail;}/*** 设置当前结点为指定结点** @param curr*/public void setCurr(Node curr) {this.curr = curr;}/*** 打印出链表中的所有元素*/public void print() {if (size == 0)System.out.println("链表中没有元素");else {this.curr = this.head.next;System.out.print("单链表中的元素:head-");while (this.curr != null) {System.out.print(this.curr.value + "-");this.curr = this.curr.next;}System.out.println("null");System.out.println("元素个数" + this.size);}}/*** 在头部结点之后插入结点** @param value*/public void addToHead(E value) {if (size == 0) {this.tail = new Node(value, null);this.head.next = this.tail;} elsethis.head.next = new Node(value, this.head.next);size++;}/*** 在尾部结点之后插入结点** @param value*/public void addToTail(E value) {if (size == 0) {this.tail = new Node(value, null);this.head.next = this.tail;} else {this.tail.next = new Node(value, null);this.tail = this.tail.next;}size++;}/*** 在当前结点后面插入结点** @param value*/public void insert(E value) {if (this.curr == this.head) {addToHead(value);} else if (this.curr == this.tail) {this.curr.next = new Node(value, null);this.tail = this.curr.next;size++;} else {this.curr.next = new Node(value, this.curr.next);size++;}}/*** 删除当前结点之后的一个结点*/public void delete() {if (this.curr == this.tail) {System.out.println("超出范围");} else if (size == 0) {System.out.println("链表中没有元素");} else if (this.curr.next == this.tail) {this.curr.next = null;this.tail = this.curr;size--;System.out.println("删除成功");} else {this.curr.next = this.curr.next.next;size--;System.out.println("删除成功");}}/*** 删除值为value的结点** @param value*/public void delete(E value) {int count = 0;if (size == 0) {System.out.println("链表中没有元素");} else if (size == 1) {if (this.tail.value.equals(value)) {this.head.next = null;this.tail = this.head;size--;}} else {this.curr = this.head;while (this.curr.next != null) {if (this.curr.next.value.equals(value)) {if (this.curr.next.next == null) {this.curr.next = null;} else {this.curr.next = this.curr.next.next;}size--;count++;System.out.println("删除成功");continue;}this.curr = this.curr.next;}System.out.println("一共删除了" + count + "个结点");}}/*** 返回第一个值为value的结点或者boolean** @param value* @return*/public Node find(E value) {if (size == 0) {System.out.println("链表中无元素");return null;}this.curr = this.head.next;while (this.curr != null) {if (this.curr.value.equals(value)) {return this.curr;}this.curr = this.curr.next;}System.out.println("链表中没有这个元素");return null;}/*** 将当前结点的值替换为value** @param value*/public void replace(E value) {if (size == 0) {System.out.println("链表中没有元素");}if (this.curr == this.head) {System.out.println("当前位置(在头部结点)不合法");}this.curr.value = value;}/*** 将所有value1替换为value2** @param value1* @param value2*/public void replace(E value1, E value2) {int count = 0;if (size == 0) {System.out.println("链表中没有元素");}this.curr = this.head.next;while (this.curr != null) {if (this.curr.value.equals(value1)) {this.curr.value = value2;count++;}this.curr = this.curr.next;}System.out.println("一共完成了" + count + "次替换");

三、完整实现

package com.zsj1105;import javax.swing.*;/*** NodeList 链表类,结点串联(首结点不存元素)* 一、属性* 1.head 头部结点* 2.tail 尾部结点* 3.curr 当前结点* 4.size 元素个数* <p>* 二、构造方法* 1.NodeList() 空参数构造方法* 2.NodeList(Node node) 由一个结点创建一个链表* <p>* 三、方法* 1.int getSize() 获取总元素个数* 2.Node getHead() 获取头部结点* 3.Node getCurr() 获取当前结点* 4.Node getTail() 获取尾部结点* 5.void setCurr() 设置当前结点* 6.void print() 打印链表中的所有元素* 7.void addToHead(E value)在头部结点后面插入结点* 8.void addToTail(E value)在尾部结点后面插入结点* 9.void insert(E value)将当前结点值设为value* 10.delete()删除当前结点之后的一个结点* 11.delete(E value)删除第一个值为value的结点* 12.find(E value)找到值为value的结点* 13.replace(E value)将当前结点的值替换为value* 14.replace(E value1, E value2)将值为value1的结点的值替换为value2*/
public class NodeList<E> {//属性private Node<E> head;  //设置头部结点private Node<E> tail;  //设置尾部结点private Node<E> curr;  //设置当前结点private int size;  //设置元素个数//构造方法/*** 无参数构造方法*/NodeList() {head = tail = curr = new Node();size = 0;}/*** 由一个结点创建链表** @param node*/NodeList(Node node) {head = tail = curr = new Node();this.size = 1;this.head.next = node;this.tail = node;}//方法/*** 获取元素个数** @return*/public int getSize() {return size;}/*** 获取头部结点** @return*/public Node<E> getHead() {return head;}/*** 获取当前结点** @return*/public Node<E> getCurr() {return curr;}/*** 获取尾部结点** @return*/public Node<E> getTail() {return tail;}/*** 设置当前结点为指定结点** @param curr*/public void setCurr(Node curr) {this.curr = curr;}/*** 打印出链表中的所有元素*/public void print() {if (size == 0)System.out.println("链表中没有元素");else {this.curr = this.head.next;System.out.print("单链表中的元素:head-");while (this.curr != null) {System.out.print(this.curr.value + "-");this.curr = this.curr.next;}System.out.println("null");System.out.println("元素个数" + this.size);}}/*** 在头部结点之后插入结点** @param value*/public void addToHead(E value) {if (size == 0) {this.tail = new Node(value, null);this.head.next = this.tail;} elsethis.head.next = new Node(value, this.head.next);size++;}/*** 在尾部结点之后插入结点** @param value*/public void addToTail(E value) {if (size == 0) {this.tail = new Node(value, null);this.head.next = this.tail;} else {this.tail.next = new Node(value, null);this.tail = this.tail.next;}size++;}/*** 在当前结点后面插入结点** @param value*/public void insert(E value) {if (this.curr == this.head) {addToHead(value);} else if (this.curr == this.tail) {this.curr.next = new Node(value, null);this.tail = this.curr.next;size++;} else {this.curr.next = new Node(value, this.curr.next);size++;}}/*** 删除当前结点之后的一个结点*/public void delete() {if (this.curr == this.tail) {System.out.println("超出范围");} else if (size == 0) {System.out.println("链表中没有元素");} else if (this.curr.next == this.tail) {this.curr.next = null;this.tail = this.curr;size--;System.out.println("删除成功");} else {this.curr.next = this.curr.next.next;size--;System.out.println("删除成功");}}/*** 删除值为value的结点** @param value*/public void delete(E value) {int count = 0;if (size == 0) {System.out.println("链表中没有元素");} else if (size == 1) {if (this.tail.value.equals(value)) {this.head.next = null;this.tail = this.head;size--;}} else {this.curr = this.head;while (this.curr.next != null) {if (this.curr.next.value.equals(value)) {if (this.curr.next.next == null) {this.curr.next = null;} else {this.curr.next = this.curr.next.next;}size--;count++;System.out.println("删除成功");continue;}this.curr = this.curr.next;}System.out.println("一共删除了" + count + "个结点");}}/*** 返回第一个值为value的结点或者boolean** @param value* @return*/public Node find(E value) {if (size == 0) {System.out.println("链表中无元素");return null;}this.curr = this.head.next;while (this.curr != null) {if (this.curr.value.equals(value)) {return this.curr;}this.curr = this.curr.next;}System.out.println("链表中没有这个元素");return null;}/*** 将当前结点的值替换为value** @param value*/public void replace(E value) {if (size == 0) {System.out.println("链表中没有元素");}if (this.curr == this.head) {System.out.println("当前位置(在头部结点)不合法");}this.curr.value = value;}/*** 将所有value1替换为value2** @param value1* @param value2*/public void replace(E value1, E value2) {int count = 0;if (size == 0) {System.out.println("链表中没有元素");}this.curr = this.head.next;while (this.curr != null) {if (this.curr.value.equals(value1)) {this.curr.value = value2;count++;}this.curr = this.curr.next;}System.out.println("一共完成了" + count + "次替换");}public static void main(String[] args) {NodeList<Integer> nodeList = new NodeList<Integer>(new Node<Integer>(520, null));
//        for (int i = 0; i < 20; i++) {
//            nodeList.addToHead(i);
//            nodeList.print();
//        }
//        System.out.println(nodeList.size);
//        System.out.println(nodeList.getTail().value);
//        for (int i = 0; i < 20; i++) {
//            nodeList.addToTail(i);
//            nodeList.print();
//        }//        nodeList.addToHead(5);
//        System.out.println(nodeList.size);
//        nodeList.addToHead(250);
//        System.out.println(nodeList.size);
//        nodeList.addToHead(270);
//        System.out.println(nodeList.size);
//        nodeList.delete(520);
//        nodeList.print();for (int i = 0; i < 5; i++) {nodeList.addToHead(i);}
//        nodeList.print();
//        nodeList.delete(0);
//        nodeList.print();nodeList.delete(520);}
}


http://chatgpt.dhexx.cn/article/ixT23FBC.shtml

相关文章

Node节点、NodeList节点列表

一.NOde节点 属性 className 设置/返回元素的class属性的属性值 innerHTML 设置/返回元素的开始标签与结束标签之间的内容 value 设置/返回文本框与文本域的内容 nextElementSibling 返回下一个紧邻的兄弟节点 previousElementSibling 返回上一个紧邻的兄弟节点 paren…

(一)类数组对象NodeList

NodeList对象的特点&#xff1a; NodeList是一种类数组对象&#xff0c;用于保存一组有序的节点。 可以通过方括号语法来访问NodeList的值&#xff0c;有item方法与length属性。 它并不是Array的实例&#xff0c;没有数组对象的方法。 通过demo简单了解一下NodeList&#xff1a…

javascript 中的nodeList理解

NodeList是一中类数组对象&#xff0c;用于保存一组有序的节点可以通过方括号来访问NodeList的值&#xff0c;他有item()方法与length属性。他并不是Array的实列&#xff0c;没有数组对象的方法。 如何拿到nodeList? var box document.getElementById(box); var nodes box.…

arvix日报0522

1. 3d semantic map Dense Semantic 3D Map Based Long-Term Visual Localization with Hybrid Features

arvix日报0521

1. [paopao] Model Free Calibration of Wheeled Robots Using Gaussian Process 没看懂 2. 西安交大2019icra&#xff0c;介绍一种semantic 3d map方法&#xff0c;基于orb-slam融合语义分割信息融合GPS定位数据提供拓扑地图的方法&#xff0c;用kitti数据库 Visual Semanti…

虚拟现实VR|增强现实AR和人工智能AI结合

1、前言&#xff1a; 近年溺水于CV苦海无法自拔&#xff0c;好久没关注VR或VR引擎了 &#xff0c;记得看2018年Unite Beijing 2018论坛的报道里&#xff0c;Unity AI与机器学习副总裁Danny Lange分享了ML-Agents的新进展。当时稍微关注了下&#xff0c;Danny Lange在加入Unity…

如何将Vufroria 、ARCore和ARkit结合使用

如果想使用Vuforia的卡牌识别&#xff0c;又想加入ARCore和ARKit的平面识别功能。解决办法就是将Vuforia与AR Foudation结合使用。 环境配置 Unity版本&#xff1a;2018.4.2f1以上版本 ARFoundation SDK版本&#xff1a;arfoundation-samples-1.5-preview 下载地址&#xff1a…

ARC

自动引用计数&#xff08;ARC&#xff09;&#xff0c;是一项为Objective - C程序在编译时提供自动内存管理的功能。ARC可以让你把注意力集中在你感兴趣的代码&#xff0c;对象图&#xff0c;和你的应用程序中的对象之间的关系&#xff0c;让你不必再花费精力在retain和release…

ARVR技术 | AR, VR, MR和XR?想搞清楚不?

AR, VR, MR&#xff0c;现在还有XR ?这些缩写是什么?它们代表什么? 让我们快速梳理一下技术术语。 首先&#xff0c;虽然你可能熟悉其中的一些术语&#xff0c;如AR和VR, 但MR和XR对许多人来说仍然是新鲜的术语。 目前的共识是&#xff0c;所有这些互补形式的现实都落在一…

VR与AR简史

点击上方“LiveVideoStack”关注我们 翻译&#xff1a;Alex 技术审校&#xff1a;周昌印 ▲扫描图中二维码或点击阅读原文▲ 了解音视频技术大会更多信息 VR的历史可以追溯到20世纪60年代。早在1961年&#xff0c;Phlico公司的Charles Comeau和James Bryan就开发了一款头显设备…

【iOS】ARC实现

ARC由以下工具来实现&#xff1a; clang&#xff08;LLVM编译器&#xff09;3.0以上objc4 Objective-C运行时库493.9以上 下面我们&#xff0c;我们将围绕clang汇编输出和objc4库的源代码探究ARC实现 1. __strong修饰符 1.1 赋值给附有__strong修饰符的变量 看下面代码 {…

arvix日报0615

百度的阿波罗开源平台架构 Data Driven Prediction Architecture for Autonomous Driving and its Application on Apollo Platform Kecheng Xu, Xiangquan Xiao, Jinghao Miao, Qi Luo Autonomous Driving vehicles (ADV) are on road with large scales. For safe and eff…

【arVix 2021】Masked Autoencoders Are Scalable Vision Learners(MAE)

文章目录 摘要引言方法maskingMAE encoderMAE decoderReconstruction target.Simple implementation. 总结广泛的影响 摘要 本文证明了遮罩自动编码器(MAE)是一种可扩展的计算机视觉自监督学习器。我们的MAE方法很简单:我们掩盖输入图像的随机补丁&#xff0c;并重建缺失的像素…

Corner Cases for Visual Perception in Automated Driving: Some Guidance on Detection ... (arVix 2021)

Corner Cases for Visual Perception in Automated Driving: Some Guidance on Detection Approaches - 自动驾驶中视觉感知的极端案例&#xff1a;检测方法的一些指导&#xff08;arVix 2021&#xff09; 摘要1. 引言2. 极端案例系统化3. 展示极端案例4. 检测方法的概念5. 关联…

大数据可视化技术栈

作者&#xff1a;微澜潮生 链接&#xff1a;https://www.zhihu.com/question/19710815/answer/18592659 来源&#xff1a;知乎 著作权归作者所有。商业转载请联系作者获得授权&#xff0c;非商业转载请注明出处。

大数据可视化期末复习

大数据可视化期末复习 题型考点范围&#xff1a;选择填空判断知识点简述题编程题绘图和例图 题型 选择题&#xff08;30分 /2’&#xff09;判断题&#xff08;10分 /1’&#xff09;填空题&#xff08;20分 /1’&#xff09;简述题&#xff08;24分 /6’&#xff09;编程题&a…

特征可视化技术(CAM)

https://zhuanlan.zhihu.com/p/269702192 CAM技术可以帮助我们理解CNN在图像中寻找目标对象的过程&#xff0c;并且可以用于可视化CNN的中间层特征&#xff0c;以及对图像分类结果的解释和可视化。CAM技术的实现比较简单&#xff0c;可以使用常用的深度学习框架如PyTorch和Te…

大数据可视化学期总结

学期总结 知识点总结个人总结 知识点总结 个人总结 在本学期的开始&#xff0c;我们接触了一门新兴的课程—大数据可视化。数据可视化&#xff0c;是关于数据视觉表现形式的科学技术研究。其中&#xff0c;这种数据的视觉表现形式被定义为&#xff0c;一种以某种概要形式抽提出…

深度神经网络可视化技术

深度神经网络可视化技术 深度学习模型表述的难点与意义深度神经网络的可视化云脑 Deepro 采用的 CNN 可视化独立单元激活的可视化图案和区域生成法云脑 Deepro 采用的 RNN 可视化LSTM 解释元与激活门统计 人工智能模型可视化实例结语 深度学习模型表述的难点与意义 深度神经网…

三维可视化技术的应用现状及发展前景

地质体的三维建模与可视化结合基础的自然地理数据信息、钻孔数据信息、物探解译模型数据信息&#xff0c;运用有关技术搭建三维空间数据场&#xff0c;选用硬件配置技术完成系统化。它应用可视化技术揭示了地下世界&#xff0c;是地质学的前沿课题研究之一。以可视化技术为基础…