链表

server/2024/10/11 9:20:04/

一.链表的概念及结构

链表是一种物理存储结构上非连续存储结构,数据元素的逻辑顺序是通过链表中的引用链接次序实现的。

value存数据,节点存下一个的地址,通过节点找下一个数据,每个节点都是一个对象

注意:

链式结构在逻辑上是连续的,但在物理上不一定连续】

现实中的节点一般都是从堆上申请出来的

从堆上申请的空间,是按照一定的策略来分配的,两次申请的空间可能连续,也可能不连续

链0pooooooooooooooooooo表有非常多种,以下情况组合起来就有八种链表结构

1.单向或双向

2.带头或不带头

3.循环或非循环

我们重点掌握两种

1.无头单向非循环列表:结构简单,一般不用来单独存数据,更多是作为其他数据结构的子结构,如哈希桶,图的邻接表等等

2.无头双向链表:在Java的框架中,LinkedList底层实现就是无头双向循环链表

二.链表的实现

1.简单创建

public class MySingleList {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;}
}

2.遍历链表

 public void display() {ListNode cur = head;while (cur != null) {System.out.print(cur.val+" ");cur = cur.next;}System.out.println();}

注意head不能丢

3.得到单链表的长度

public int size(){int count = 0;ListNode cur = head;while (cur != null) {count++;cur = cur.next;}return count;}

4.查找关键字key是否在单链表

  public boolean contains(int key){ListNode cur = head;while (cur != null) {if(cur.val == key) {return true;}cur = cur.next;}return false;}

5.头插法

 public void addFirst(int data){ListNode node = new ListNode(data);node.next = head;head = node;}

6.尾插法

 public void addLast(int data){ListNode node = new ListNode(data);ListNode cur = head;if(cur == null) {head = node;return;}//找到链表的尾巴  注意是cur.next 不是curwhile (cur.next != null) {cur = cur.next;}cur.next = node;}

7.任意位置插入,第一个数据节点为0号下标

   public void addIndex(int index,int data){if(index < 0 || index > size()) {System.out.println("index 不合法");return;}if(index == 0) {addFirst(data);return;}if(index == size()) {addLast(data);return;}ListNode cur = findIndexSubOne(index);ListNode node = new ListNode(data);node.next = cur.next;cur.next = node;}/*** 找到要删除节点位置的前一个节点* @param index* @return*/private ListNode findIndexSubOne(int index) {ListNode cur = head;while (index-1 != 0) {cur = cur.next;index--;}return cur;}

8.删除第一次出现关键字为key的节点

 public void remove(int key){if(head == null) {return;}//单独删除头节点if(head.val == key) {head = head.next;return;}ListNode cur = searchPrev(key);if(cur == null) {System.out.println("没有你要删除的数字");return;}ListNode del = cur.next;cur.next = del.next;}/*** 找到关键字 key的前驱* @param key* @return*/private ListNode searchPrev(int key) {ListNode cur = head;while (cur.next != null) {if(cur.next.val == key) {return cur;}cur = cur.next;}return null;}

9.删除所有值为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;}}

10.清空链表

    public void clear() {this.head = null;}


http://www.ppmy.cn/server/16179.html

相关文章

Three.js入门学习笔记

学习资料&#xff1a; 【Three.js】Three.js快速上手教程_three.module.js-CSDN博客 2024年了&#xff0c;是该学学Three.js了_three.js 2024-CSDN博客 一、three.js简介 three.js是JavaScript编写的WebGL第三方库。 three.js&#xff0c;webGL&#xff0c;openGL三者的关…

Vivado-IP-DDS and Testbench Learning

DDS内部结构 实现流程 首先新建一个工程&#xff0c;创建bd文件&#xff0c;添加DDS Compiler核&#xff0c;此处不多赘述 Block Design 在观测输出的信号时&#xff0c;需要将最高位符号位的信号取反&#xff0c;这样才能输出正弦波&#xff0c;否则输出的波形如下图所示 将t…

Java虚拟机(jvm)常见问题总结

1.电脑怎样认识我们编写的Java代码 首先先了解电脑是二进制的系统&#xff0c;他只认识 01010101比如我们经常要编写 HelloWord.java 电脑是怎么认识运行的HelloWord.java是我们程序员编写的&#xff0c;我们人可以认识&#xff0c;但是电脑不认识 Java文件编译的过程 1. 程…

JavaScript基础学习(5.操作符)

⭐ 作者简介&#xff1a;码上言 ⭐ 代表教程&#xff1a;Spring Boot vue-element 开发个人博客项目实战教程 ⭐专栏内容&#xff1a;个人博客系统 ⭐我的文档网站&#xff1a;http://xyhwh-nav.cn/ ⭐微信公众号&#xff1a;码上言 文章目录 操作符1. 一元操作符1.1. 递…

不要小看使用说明书,它才是提高成交率的秘诀

在产品推广和销售环节中&#xff0c;许多企业可能忽略了一个非常重要但常被低估的环节——使用说明书的作用。使用说明书&#xff0c;这本附随每件产品的“小书”&#xff0c;往往是用户了解和使用产品的第一步。事实上&#xff0c;一个清晰、详尽、易懂的使用说明书能够显著提…

Echarts-丝带图

Echarts-丝带图 demo地址 打开CodePen 什么是丝带图&#xff1f; 丝带图是Power BI中独有额可视化视觉对象&#xff0c;它的工具提示能展示指标当期与下期的数据以及排名。需求&#xff1a;使用丝带图展示"2022年点播订单表"不同月份不同点播套餐对应订单数据。 …

Mybatis入门,day2,动态SQL

Mybatis入门&#xff0c;day2&#xff0c;动态SQL 文章目录 Mybatis入门&#xff0c;day2&#xff0c;动态SQL前言一、为什么要实现动态SQL二、使用步骤1.where和if2.set和if3.foreach方法 前言 动态 SQL 是 MyBatis 的强大特性之一。在 JDBC 或其它类似的框架中&#xff0c;开…

【iOS开发】(四)react Native第三方组件五个20240419-20

react native 外的 第三方组件 目录标题 react native 外的 第三方组件&#xff08;一&#xff09;与rn核心组件的使用步骤区别&#xff1a;&#xff08;二&#xff09;第三方组件概览1 WebView2 Picker3 Swiper4 AsyncStorage5 Geolocation6 Camera (三)详细学习1 WebViewCoco…