当前位置: 首页 > news >正文

数据结构(Java实现)LinkedList与链表(下)


在这里插入图片描述
**加粗样式
**
在这里插入图片描述


在这里插入图片描述

在这里插入图片描述


在这里插入图片描述
在这里插入图片描述

在这里插入图片描述


在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述


在这里插入图片描述

在这里插入图片描述

在这里插入图片描述


在这里插入图片描述

在这里插入图片描述

在这里插入图片描述


在这里插入图片描述

在这里插入图片描述

在这里插入图片描述


在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述


在这里插入图片描述

在这里插入图片描述


在这里插入图片描述
结论
让一个指针从链表起始位置开始遍历链表,同时让一个指针从判环时相遇点的位置开始绕环运行,两个指针都是每次均走一步,最终肯定会在入口点的位置相遇。

在这里插入图片描述


LinkedList的模拟实现
单个节点的实现
在这里插入图片描述


在这里插入图片描述

在这里插入图片描述


尾插
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
运行结果如下:
在这里插入图片描述


在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述


在这里插入图片描述

在这里插入图片描述
也可以暴力使用
在这里插入图片描述


全部代码
MyLinkedList

public class MyLinkedList {static class ListNode{public  int val;public ListNode prev;public ListNode next;public ListNode(int val) {this.val = val;}}public  ListNode head;public  ListNode last;//链表的长度public int size(){int len=0;ListNode cur=head;while(cur!=null){cur=cur.next;len++;}return len;}//打印链表public  void dispaly(){ListNode cur=head;while(cur!=null){System.out.print(cur.val+" ");cur=cur.next;}System.out.println();}//查找关键字key是否在链表中public boolean contains(int key){ListNode cur=head;while(cur!=null){if(cur.val==key){return true;}cur=cur.next;}return false;}//头插
public void addFirst(int data){ListNode node=new ListNode(data);//空链表if(head==null){head=node;last=node;return;}//一般情况node.next=head;head.prev=node;head=node;}//尾插public void addLast(int data){ListNode node=new ListNode(data);//空链表if(head==null){head=node;last=node;return;}//一般情况last.next=node;node.prev=last;last=node;}
//无论是头插还是尾插,只要是空链表,那么新增节点的next和prev都引用自身//任意位置的插入,在位置后面插入public void AddIndex(int index,int data){ListNode node=new ListNode(data);if(index<0||index>size()){throw new IndexOutOfBounds("任意位置插入,坐标非法,非法值为"+index);}if(index==0){addFirst(data);return;}if(index==size()){addLast(data);return;}//一般情况ListNode cur=head;while(index!=0){cur=cur.next;index--;}node.next=cur;cur.prev.next=node;node.prev=cur.prev;cur.prev=node;}//删除第一次出现关键字为key的节点public void remove(int key){//链表为空if(head==null){return;}//只有一个节点并且该节点值为keyif(head.next==null&&head.val==key){head.prev=null;head.next=null;return;}//头节点if(head.val==key){head.next.prev=null;head=head.next;return;}//尾节点if(last.val==key){last.prev.next=null;last=last.prev;return;}//一般情况ListNode cur=head;while(cur!=null){if(cur.val==key){cur.prev.next=cur.next;cur.next.prev=cur.prev;return;}cur=cur.next;}}//删除所有出现关键字为key的节点public void removeAll(int key){//链表为空if(head==null){return;}//只有一个节点并且该节点值为keyif(head.next==null&&head.val==key){head.prev=null;head.next=null;return;}//头节点if(head.val==key){head.next.prev=null;head=head.next;}//尾节点if(last.val==key){last.prev.next=null;last=last.prev;}//一般情况ListNode cur=head;while(cur!=null){if(cur.val==key){cur.prev.next=cur.next;cur.next.prev=cur.prev;}cur=cur.next;}}//clearpublic  void clear(){
/*        ListNode cur=head;while(cur!=null){ListNode curNext=cur.next;//存储下一个节点,方便下次循环使用cur.prev=null;cur.next=null;cur=curNext;//更新当前节点}*/head=null;last=null;//使头节点和尾节点的地址置空}}

IndexOutOfBounds

public class IndexOutOfBounds extends  RuntimeException{public IndexOutOfBounds() {}public IndexOutOfBounds(String message) {super(message);}
}

Test1

public class Test1 {public static void main(String[] args) {MyLinkedList myLinkedList=new MyLinkedList();myLinkedList.addFirst(34);myLinkedList.addFirst(3);myLinkedList.addFirst(4);myLinkedList.addFirst(340);myLinkedList.addFirst(344);myLinkedList.dispaly();System.out.println(myLinkedList.size());System.out.println(myLinkedList.contains(4));System.out.println("我是分割线");myLinkedList.addLast(-12);myLinkedList.addLast(-121);myLinkedList.addLast(-1222);myLinkedList.dispaly();myLinkedList.AddIndex(3,999);myLinkedList.dispaly();// myLinkedList.AddIndex(55,999);System.out.println("我是分割线");myLinkedList.remove(999);myLinkedList.dispaly();myLinkedList.remove(344);myLinkedList.dispaly();myLinkedList.addFirst(-12);myLinkedList.addLast(-12);myLinkedList.dispaly();myLinkedList.removeAll(-12);myLinkedList.dispaly();System.out.println("我是分割线");myLinkedList.clear();myLinkedList.dispaly();System.out.println("证明有空行");}
}

什么是LinkedList
LinkedList的底层是双向链表结构(链表后面介绍),由于链表没有将元素存储在连续的空间中,元素存储在单独的节点中,然后通过引用将节点连接起来了,因此在在任意位置插入或者删除元素时,不需要搬移元素,效率比较高。


LinkedList的使用
在这里插入图片描述


LinkedList的其他常用方法
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述


LinkedList的遍历
在这里插入图片描述


ArrayList和LinkedList的区别
在这里插入图片描述


http://www.lryc.cn/news/141072.html

相关文章:

  • linux查看正在运行的nginx在哪个文件夹当中
  • Vue实现Excel表格中按钮增加小数位数,减少小数位数功能,多用于处理金融数据
  • 自然语言处理(一):词嵌入
  • 【HSPCIE仿真】HSPICE仿真基础
  • 二、前端监控之方案调研
  • npm 创建 node.js 项目
  • JMeter性能测试(上)
  • 自定义date工具类 DateUtils.java
  • Linux(Ubuntu)安装docker
  • Apache Poi 实现Excel多级联动下拉框
  • 常见的 HTML<meta> 标签的 name 属性及其作用
  • 【网络安全】理解报文加密、数字签名能解决的实际问题
  • linux中安装nodejs,卸载nodejs,更新nodejs
  • 浅谈Python网络爬虫应对反爬虫的技术对抗
  • 代理池在过程中一直运行
  • 基于Java+SpringBoot+Vue前后端分离党员教育和管理系统设计和实现
  • 【flutter直接上传图片到阿里云OSS】
  • 【MySQL系列】表的内连接和外连接学习
  • C语言日常刷题 3
  • .net6中, 用数据属性事件触发 用httpclient向服务器提交Mes工单
  • sin(A)的意义
  • ctfshow-web14
  • 数据结构—循环队列(环形队列)
  • vue3 实现按钮权限管理
  • C语言练习4(巩固提升)
  • 将AI融入CG特效工作流;对谈Dify创始人张路宇;关于Llama 2的一切资源;普林斯顿LLM高阶课程;LLM当前的10大挑战 | ShowMeAI日报
  • Vue2学习笔记のVue中的ajax
  • C# 使用NPOI操作EXCEL
  • 分布式 - 服务器Nginx:一小时入门系列之 return 指令
  • 【Linux】ext4和xfs扩大,缩小lv后,无法识别如何操作