【Java数据结构】单向 不带头 非循环 链表实现

 模拟实现LinkedList:下一篇文章

LinkedList底层是双向、不带头结点、非循环的链表

/*** LinkedList的模拟实现*单向 不带头 非循环链表实现*/
class SingleLinkedList {class ListNode {public int val;public ListNode next;public ListNode(int val) {this.val = val;}}public ListNode head;//永远指向头结点//创建链表public void createList() {ListNode node1 = new ListNode(1);ListNode node2 = new ListNode(2);ListNode node3 = new ListNode(3);ListNode node4 = new ListNode(4);ListNode node5 = new ListNode(5);node1.next = node2;node2.next = node3;node3.next = node4;node4.next = node5;this.head = node1;}//显示public void display() {while (head != null) {System.out.print(head.val + " ");head = head.next;//head往后移}}//得到单链表的长度public int size() {ListNode cur = head;int count = 0;while (cur != null) {count++;cur = cur.next;}return count;}//清空public void clear() {this.head = null;}//头插法public void addFirst(int data) {ListNode node = new ListNode(data);node.next = head;head = node;}//尾插法public void addLast(int data) {ListNode cur = head;ListNode node = new ListNode(data);if (head == null) {head = node;}while (cur.next != null) {cur = cur.next;}cur.next = node;//这时cur就是尾巴节点}//在任意位置插入,第一个数据节点为0的下标public void addIndex(int index, int data) {ListNode node = new ListNode(data);int len = size();//0.判断index位置是否合法if (index < 0 || index > len) {return;//也可以抛异常}//1.先找到index-1的位置  下面有个findIndex方法ListNode cur = findIndex(index);//2.插入数据node.next = cur.next;cur.next = node;}private ListNode findIndex(int index) {ListNode cur = head;while (index - 1 != 0) {cur = cur.next;index--;}return cur;//index-1位置的节点}//查找是否包含关键字key是否在单链表当中public boolean contains(int key) {ListNode cur = head;while (cur != null) {if (cur.val == key) {return true;}cur = cur.next;}return false;}//删除第一次出现关键字为key的节点public void remove(int key) {if(head==null){return;}if(head.val==key){head=head.next;return;}ListNode prev = searchPrev(key);if (prev== null) {System.out.println("没有这个数据");return;}ListNode del=prev.next;prev.next=del.next;}private ListNode searchPrev(int key){ListNode prev=head;while(prev.next!=null){if(prev.next.val==key){return prev;}else{prev=prev.next;}}return null;}//删除所有值为key的节点public void removeAllkey(int key) {if(head==null){return;}ListNode cur=head.next;ListNode prev=head;while(cur!=null){if(cur.val==key){prev.next=cur.next;cur=cur.next;}else{prev=cur;cur=cur.next;}}if(head.val==key){head=head.next;}}
}
public class Test {public static void main(String[] args) {SingleLinkedList singleLinkedList=new SingleLinkedList();singleLinkedList.createList();singleLinkedList.display();}
}

此处只调用了createList()和display()。需要其他方法的自己可以在main中调用哦

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/456566.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

20240203在Ubuntu20.04.6下配置stable-diffusion-webui.git

20240203在Ubuntu20.04.6下配置stable-diffusion-webui.git 2024/2/3 11:55 【结论&#xff1a;在Ubuntu20.04.6下&#xff0c;生成512x512分辨率的图像&#xff0c;大概需要11秒钟&#xff01;】 前提条件&#xff0c;可以通过技术手段上外网&#xff01;^_首先你要有一张NVID…

【Spring基础】从0开始学习Spring(2)

前言 在上篇文章&#xff0c;我已经讲了Spring中最核心的知识点&#xff1a;IoC&#xff08;控制反转&#xff09;以及DI&#xff08;依赖注入&#xff09;。这篇文章&#xff0c;我将讲一下关于Spring框架中的其它比较琐碎但是又还是挺重要的知识点&#xff0c;因此&#xff…

Windows中如何使用 Anaconda 和 gempy地质建模

GemPy是一个免费开源的Python软件包&#xff0c;主要用于建立三维地质模型。以下是windows下GemPy的安装过程。 一、&#xff08;可选步骤&#xff09;N卡加速 如果使用的是英伟达的RTX显卡&#xff0c;可以去N卡官网下载cuda安装包以启用GPU加速。 首先检查显卡支持的CUDA版…

六轴机器人奇异点

1 奇异点说明 有着6个自由度的KUKA机器人具有3个不同的奇点位置。即便在给定状态和步骤顺序的情况下,也无法通过逆向变换(将笛卡尔坐标转换成极坐标值)得出唯一数值时,即可认为是一个奇点位置。这种情况下,或者当最小的笛卡尔变化也能导致非常大的轴角度变化时,即为奇点位置…

6-2、T型加减速计算简化【51单片机+L298N步进电机系列教程】

↑↑↑点击上方【目录】&#xff0c;查看本系列全部文章 摘要&#xff1a;本节介绍简化T型加减速计算过程&#xff0c;使其适用于单片机数据处理。简化内容包括浮点数转整型数计算、加减速对称处理、预处理计算 一、浮点数转整型数计算 根据上一节内容已知 常用的晶振大小…

C++ PE文件信息解析

尝试解析PE文件结构, 于是编写了此PE信息助手类, 暂时完成如下信息解析 1.导出表信息(Dll模块, 函数) 2.导入表信息(Dll模块, 函数) 3.资源表信息(字符串表, 版本信息, 清单信息) CPEHelper.h #pragma once// // brief: PE文件解析助手类 // copyright: Copyright 2024 Flame…

ai扩图怎么玩?分享玩法和工具!

AI扩图是近年来兴起的图片处理技术&#xff0c;它通过人工智能算法&#xff0c;将一张小图片放大并修复&#xff0c;使其呈现出高清晰度的效果。这项技术对于我们日常的图片处理工作来说&#xff0c;无疑是一大福音。本文将为你详细解析AI扩图的玩法&#xff0c;以及介绍一些实…

力扣经典题:另一棵树的子树

直接省事一点&#xff0c;炒一下100题的代码&#xff0c;分别讨论单节点以及双空节点的情况&#xff0c;然后进行递归调用 bool isSameTree(struct TreeNode* p, struct TreeNode* q) {if(pNULL&&qNULL){return true;}if(pNULL||qNULL){return false;}if(p->val!q-…

移动Web——Bootstrap

1、Bootstrap-简介 Bootstrap是由Twitter公司开发维护的前端UI框架&#xff0c;它提供了大量编写好的CSS样式&#xff0c;允许开发者结合一定HTML结构及JavaScript&#xff0c;快速编写功能完善的网页及常见交互效果 <!DOCTYPE html> <html lang"en"> &…

should be also和should also be

will also be 是正确的 但老师和新概念的两个说法都没有错. will also be 是固定搭配.就好像will not be一样, 限定词加在be前.老师说的是陈述之类的句型 Nbe动词alson/adj/动词短语.例&#xff1a;He is also good at physic. should be also还是should also be also应该插在…

四、树立边界(Negotiating Boundaries)

2.Negotiating Boundaries 二、协商边界 Other people are the biggest obstacles of focus.A colleague wants to chat.You get a WeChat message about that party tonight.You hear the familiar ping of new emails that demand a look. 他人是保持专注的最大障碍。同事想要…

【机器学习300问】23、什么是主动学习?

一、带标签的数据很难获得 机器学习中&#xff0c;比如监督学习需要带有标签的训练样本才能得到模型&#xff0c;然而在以下几种场景中去获取带有标签的数据是很难的&#xff1a; 自动驾驶场景&#xff1a;对自动驾驶汽车收集的高清地图数据或实时摄像头数据进行标注&#xff…