数据结构链表

news/2024/9/22 21:36:21/

数据结构链表

链表

1)链表的概念及结构:
链表是一种物理存储结构上非连续存储结构,数据元素的逻辑顺序是通过链表中的引用链接次序实现的。
2)实际中链表的结构非常多样,以下情况组合起来就有8种链表结构:
单向、双向 带头、不带头 循环、非循环 组合成的。我门需要掌握的为无头单向非循环链表和无头双向链表
无头单向非循环链表:结构简单,一般不会单独用来存数据。实际中更多是作为其他数据结构的子结构,如哈希桶、图的邻接表等等。另外这种结构在笔试面试中出现很多。
在这里插入图片描述
无头双向链表
在这里插入图片描述

链表的实现

java">// 1、无头单向非循环链表实现
public class SingleLinkedList {//头插法
public void addFirst(int data);//尾插法
public void addLast(int data);//任意位置插入,第一个数据节点为0号下标
public boolean addIndex(int index,int data);//查找是否包含关键字key是否在单链表当中
public boolean contains(int key);//删除第一次出现关键字为key的节点
public void remove(int key);//删除所有值为key的节点
public void removeAllKey(int key);//得到单链表的长度
public int size();public void display();public void clear();}

代码如下

java">public class MySingleList implements IList{//节点的内部类static 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(12);ListNode node2 = new ListNode(23);ListNode node3 = new ListNode(34);ListNode node4 = new ListNode(45);ListNode node5 = new ListNode(56);node1.next = node2;node2.next = node3;node3.next = node4;node4.next = node5;this.head=node1;}@Overridepublic void addFirst(int data) {ListNode node = new ListNode(data);if(this.head==null){this.head= node;}else{node.next = this.head;this.head=node;}}@Overridepublic void addLast(int data) {ListNode cur = this.head;ListNode node  = new ListNode(data);if(this.head==null){this.head = node;}else{while(cur.next!=null){cur = cur.next;}cur.next = node;}}@Overridepublic void addIndex(int index, int data) throws ArithmeticException{//自定义异常if(index<0||index>size()){throw new ArithmeticException("抛出下标"+index+"异常");}if (index == 0) {addFirst(data);return;}if (index == size()) {addLast(data);return;}ListNode cur = searchPrev(index);ListNode node = new ListNode(data);node.next = cur.next;cur.next = node;}private ListNode searchPrev(int index){ListNode cur = this.head;int count = 0;while(index-1!=count){cur= cur.next;index--;}return cur;}@Overridepublic boolean contains(int key) {ListNode cur = this.head;while(cur!=null){if(cur.val==key){return true;}cur = cur.next;}return false;}@Overridepublic void remove(int key) {if(this.head==null){//一个节点都没有return;}if(this.head.val==key){this.head = this.head.next;return;}//1.找到前驱ListNode cur = findPrev(key);//2.判断返回值是否为空if(cur==null){System.out.println("没有你要删除的元素");return;}//3.删除操作ListNode del = cur.next;cur.next = del.next;}//找到关键字key前一个地址private ListNode findPrev(int key){ListNode cur = this.head;while(cur.next!=null){if(cur.next.val==key){return cur;}cur = cur.next;}return null;}@Overridepublic void removeAllKey(int Key) {if (this.head == null) {return;}ListNode prev = head;ListNode cur = head.next;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;}}@Overridepublic int size() {int count = 0;ListNode cur = this.head;while(cur!=null){count++;cur = cur.next;}return count;}@Overridepublic void clear() {}@Overridepublic void display() {ListNode cur = this.head;while(cur!=null){System.out.println(cur.val +" ");cur = cur.next;}}}

关于无头双向链表请看下次发表文章。


http://www.ppmy.cn/news/1455629.html

相关文章

Stable Diffusion学习记录

文章目录 前言电脑配置推荐环境搭建下载地址安装步骤步骤一&#xff0c;打开下载的秋叶整合包&#xff0c;路径秋叶整合包/sd-wenui-aki步骤二&#xff0c;打开下载好的sd-webui-aki-v4.8.7解压包 Stable Diffusion软件配置&#xff0c;插件安装&#xff0c;模型下载Stable Dif…

未授权访问

未授权访问是系统对用户限制不全&#xff0c;或者无限制&#xff0c;可以让任意用户或者限制访问用户&#xff0c;访问到需要权限认证的地址。未授权访问通常是会泄露用户信息&#xff0c;系统信息。某些服务和系统中&#xff0c;未授权访问还可以执行系统命令&#xff0c;操作…

OPENAI中Assistants API的实现原理及示例代码python实现

OPENAI中Assistants API的实现原理及示例代码 前言 OPENAI是一家人工智能公司&#xff0c;致力于研究和开发人工智能技术。其中&#xff0c;Assistants API是OPENAI推出的一项人工智能服务&#xff0c;可以帮助开发者快速构建智能助手。本文将介绍Assistants API的实现原理&a…

Retrofit库中,Call​;Retrofit使用举例;@GET,@PUT区别;

目录 在Retrofit库中,Call​ Retrofit使用举例 Call> listRepos(@Path("user") String user); Call是什么:

CVPR2024论文整理及最新算法整理

CVPR 2024 | 腾讯优图实验室20篇论文入选&#xff0c;含图文多模态大模型、高分辨视觉分割、跨模态生成、人脸识别等研究方向-腾讯云开发者社区-腾讯云 (tencent.com) CVPR2024满分论文出炉&#xff01;分割万物再次火爆AI界-CSDN博客 【综述】三维点云深度学习算法综述,sota…

Spring 如何解决 Bean 循环依赖

循环依赖解释 bean A 属性注入时依赖bean B &#xff0c;并且bean B属性注入时也依赖bean A &#xff0c;造成 bean A 和bean B 都无法完成初始化问题&#xff0c;形成了闭环。 注意 项目中存在Bean的循环依赖&#xff0c;是Bean对象职责划分不明确、代码质量不高的表现&#…

【大数据】学习笔记

文章目录 [toc]NAT配置IP配置SecureCRT配置PropertiesTerminal Java安装环境变量配置 Hadoop安装修改配置文件hadoop-env.shyarn-env.shslavescore-site.xmlhdfs-site.xmlmapred-site.xmlyarn-site.xml 环境变量配置 IP与主机名映射关系配置hostname配置映射关系配置 关闭防火墙…

新修订的《中华人民共和国保守国家秘密法》新增和修改的内容不包括( )

新修订的《中华人民共和国保守国家秘密法》新增和修改的内容不包括&#xff08; &#xff09; 点击查看答案 A.旗帜鲜明将党的领导写入法律B.加快提升保密科技创新能力 C.把保密宣传教育摆到重要位置 D.细化商业秘密保密管理 新修订的《中华人民共和国保守国家秘密法》对解密…