文章目录

  • 链表
    • 1. 基本介绍
      • 1.1 定义
      • 1.2 链表分类
      • 3.不带头非循环单链表CURD
      • 4.不带头非循环双向链表CURD

链表

1. 基本介绍

1.1 定义

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

如图所示:

1.2 链表分类

单向、双向;带头、不带头;循环、非循环
重点:单向不带头非循环、双向不带头非循环(集合类底层)

如图:单项带头非循环链表结构

如图:单向带头循环链表结构

如图:双向不带头循环链表结构

3.不带头非循环单链表CURD

代码展示:

package demo3;/*** @author zq* 不带头非循环单链表相关操作*/
public class MySingleList {class Node {public int val;//存储数据public Node next;//存储下一个节点地址public Node(int val) {//不知道下一个节点位置,只需要valthis.val = val;}}public Node head;//代表当前头节点的引用//遍历链表//创建链表public void createLink(){Node node1 = new Node(12);Node node2 = new Node(45);Node node3 = new Node(23);Node node4 = new Node(90);node1.next = node2;node2.next = node3;node3.next = node4;head = node1;}//遍历链表,利用head遍历链表public void display() {while (head != null){System.out.println(head.val);head = head.next;}}//查找是否包含关键字key是否在单链表当中public boolean contains(int key){Node cur = head;while (cur != null){if (cur.val == key){return true;}cur = cur.next;}return false;}//头插法public void addFirst(int data){Node node = new Node(data);node.next = head;head = node;}//尾插法,需判断head是否为空public void addLast(int data){Node node = new Node(data);if (head == null) {head = node;return;}Node cur = head;while (cur.next != null) {cur = cur.next;}cur.next = node;}//任意位置插入,第一个数据节点为0号下标public void addIndex(int index,int data){checkindex(index);if (index == 0){addFirst(data);return;}if (index == size()){addLast(data);return;}Node cur = findIndexSubOne(index);Node node = new Node(data);node.next = cur.next;cur.next = node;}/*找到index-1处的节点*/private Node findIndexSubOne(int index){Node cur = head;int count = 0;while (count != index -1 ){cur = cur.next;count++;}return cur;}private void checkindex(int index){if (index<0||index>size()){throw new IndexOutOfException("index位置不合法");}}//删除第一次出现关键字为key的节点public void remove(int key){if (head.val == key){head = head.next;return;}Node cur = searchPrev(key);if (cur == null){return;}cur.next = cur.next.next;}//删除所有值为key的节点public void removeAllKey(int key){if (head == null){return;}//如果前俩个节点值均为key时。// 或者用if放到最后,直接删掉漏掉的节点while (head.val==key){head = head.next;}Node prev = head;Node cur = head.next;while (cur != null){if (cur.val == key){prev.next = cur.next;cur = cur.next;}else{prev = cur;cur = cur.next;}}}//找到关键字key的前一个节点private Node searchPrev(int key){if (head== null){return null;//链表为空时}Node cur = head;while (cur.next!= null){if (cur.next.val == key){return cur;}cur = cur.next;}return null;//没有需要删除的节点}//得到单链表的长度public int size(){int count = 0;Node cur = head;while (cur != null){count++;cur = cur.next;}return count;}/*保证列表中每一个节点都被回收*/public void clear() {ListNode cur = head;while(cur != null){ListNode curNext = cur.next;cur.prev = null;cur.next = null;cur = curNext;}head = null;last = null;}
}

4.不带头非循环双向链表CURD

代码展示:

package demo4;import demo3.MySingleList;import java.util.List;/*** @author zq* LinkedList的实现* 双向链表*/
public class MyLinkedList {static class ListNode{ListNode prev;ListNode next;int val;public ListNode(int val) {//不知道下一个节点位置,只需要valthis.val = val;}}public MyLinkedList.ListNode head;//代表当前头节点的引用public MyLinkedList.ListNode last;//代表最后节点的引用//头插法public void addFirst(int data){ListNode node = new ListNode(data);if (head == null) {head = node;last = node;}else{node.next = head;node.prev = null;head.prev = node;head = node;}}//尾插法public void addLast(int data){ListNode node = new ListNode(data);if (head == null){head = node;last = node;}else{last.next = node;node.prev = last;node.next = null;last = node;}}//任意位置插入,第一个数据节点为0号下标public void addIndex(int index,int data){ListNode node = new ListNode(data);checkindex(index);if (index==0){addFirst(data);return;}if (index==size()){addLast(data);return;}ListNode cur = findIndex(index);node.next = cur;cur.prev.next = node;node.prev = cur.prev;cur.prev = node;}//判断插入位置是否合法private void checkindex(int index){if (index<0||index>size()){throw new IndexOutOfException("index位置不合法");}}/*找到index处的节点*/private ListNode findIndex(int index){ListNode cur = head;while (index != 0){cur = cur.next;index--;}return cur;}//查找是否包含关键字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){ListNode cur = head;while (cur != null) {if (cur.val == key) {//删除的是头节点if(cur == head) {head=head.next;//如果只有一个节点时处理方式//否则会有空指针异常if (head !=null) {head.prev = null;}}else{//中间,尾巴都能用此行代码。cur.prev.next = cur.next;if (cur.next != null){//不是尾巴节点cur.next.prev = cur.prev;}else {last = cur.prev;}}return;}cur = cur.next;}}//删除所有值为key的节点public void removeAllKey(int key){ListNode cur = head;while (cur != null) {if (cur.val == key) {//删除的是头节点if(cur == head) {head=head.next;//如果只有一个节点时处理方式//否则会有空指针异常if (head !=null) {head.prev = null;}}else{//中间,尾巴都能用此行代码。cur.prev.next = cur.next;if (cur.next != null){//不是尾巴节点cur.next.prev = cur.prev;}else {last = cur.prev;}}}cur = cur.next;}}//得到单链表的长度public int size(){int len = 0;ListNode cur = head;while (cur != null){cur = cur.next;len++;}return len;}public void display(){ListNode cur = head;while (cur !=null){System.out.print(cur.val + " ");cur = cur.next;}}public void clear(){head = null;}
}

数据结构——链表(java)相关推荐

  1. 猴子选大王 java_基于java数据结构链表写的猴子选大王

    [实例简介] 基于java数据结构链表写的猴子选大王,其实就是一个约瑟夫环问题,采用java数据结构链表写的.有点小问题.当输入一只猴子,报数为1时删除会出错.没有实现动态显示猴子的添加和删除. [实 ...

  2. Java数据结构链表面试题 作者:哇塞大嘴好帥(哇塞大嘴好帅) --持续更新

    作者:哇塞大嘴好帥(哇塞大嘴好帅) Java数据结构链表面试题 4.1.查询链表有效数据个数 //判断有效数据个数 public int validDate(){//创建临时变量NodeDate no ...

  3. 浅谈:数据结构之单链表,java代码演示单链表

    单链表 本文是观看尚硅谷韩老师视频学习总结,部分来源网络. 单链表介绍 链表是一种物理存储单元上非连续.非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的.链表由一系列结点(链表中每 ...

  4. java数据结构-链表详解

    文章目录 1.数据结构-链表详解 1.1单链表 1.1.1单链表节点的尾部添加 1.1.2单链表节点的自动排序添加 1.1.3单链表节点的修改 1.1.4单链表节点的删除 1.2单链表面试题 1.2. ...

  5. 《LeetCode力扣练习》第160题 相交链表 Java

    <LeetCode力扣练习>第160题 相交链表 Java 一.资源 题目: 给你两个单链表的头节点 headA 和 headB ,请你找出并返回两个单链表相交的起始节点.如果两个链表不存 ...

  6. 数据结构 - 链表 - 面试中常见的链表算法题

    数据结构 - 链表 - 面试中常见的链表算法题 数据结构是面试中必定考查的知识点,面试者需要掌握几种经典的数据结构:线性表(数组.链表).栈与队列.树(二叉树.二叉查找树.平衡二叉树.红黑树).图. ...

  7. 数据结构链表之符号表,Python3实现——8

    数据结构链表之符号表 符号表的介绍 之前章节介绍的顺序表和链表都是一个节点储存一个元素的表,但在日常生活中我们还有很多一次需要储存成对或多个值的情况,例如: 符号表最主要的目的将一对元素,用一个键和一 ...

  8. 数据结构链表例程_如何掌握RxJava例程的四个结构

    数据结构链表例程 by Ayusch Jain 通过Ayusch Jain 如何掌握RxJava例程的四个结构 (How to get a grip on the four constructs of ...

  9. 递归下降分析器的设计java_数据结构(Java版)教与学(48和60学时教学大纲)

    baba<数据结构>课程教学大纲课程中文名称: 数据结构.课程英文名称:Data Structures.课程类别:专业基础课 必修.课程学分数:4(16学时为1学分)课程学时数:讲课48学 ...

最新文章

  1. 使用Redis来实现LBS的应用
  2. my_free mysql源代码_MySQL源代码管中窥豹(一)_MySQL
  3. Kubernetes安装之五:配置kubectl客户端
  4. inurllay old.php id,搜索技巧
  5. Windows 7 下IIS 7.5 结合Zend构建PHP集成开发环境
  6. 模板:Link Cut Tree(LCT)
  7. 若依前后端部署之后验证码不显示
  8. CompletableFuture java 8新增加异步处理
  9. informix长事务的处理方式
  10. list 分组求和_数据分析-python-分组聚合-2
  11. 关于为什么需要设置request.setCharacterEncoding以及适用范围问题
  12. 群体智能优化算法之蟑螂算法((Cockroach Swarm Optimization,CSO)
  13. Go并发模式:管道与取消
  14. PHP与MySQL动态网站开发:第4版
  15. 值得关注的开源软件推荐
  16. 规避VMware虚拟机检测
  17. Oracle StorageTek磁带库产品线或将终结
  18. 搜狗站长工具:索引量与收录量的解释,它等同于site的收录吗?
  19. Java流程控制练习题
  20. 微信开发者解除绑定微信公众号的方法,亲测有效

热门文章

  1. R语言批量将PDF中表格,转化为excel
  2. mpvue 模板_使用mpvue创建项目以及总结
  3. 创造最大效率,团队成员的重要性【成长】【体会】
  4. 【Google】25匹马的角逐
  5. Android 应用Google Play广告推广渠道来源区分功能的接入
  6. echarts 实现圆柱体柱形图效果
  7. Adobe的网页编辑软件Dreamweaver 2021版本下载与安装教程
  8. 【中秋第一弹】别具一格的短信祝福
  9. 使用自定义注解实现Redis分布式锁
  10. 【点云预处理】N种点云数据数据预处理方法 — 持续总结和更新(二)