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

【Java】/* 双向链表 - 底层实现 */

【难点】:remove、removeAllKey

一、IList

package bagfive;/*** Created with IntelliJ IDEA.* Description:* User: tangyuxiu* Date: 2024-08-21* Time: 20:30*/
public interface IList<E> {//头插法void addFirst(E data);//尾插法void addLast(E data);//任意位置插入,第一个数据节点为0号下标void addIndex(int pos,E data);//查找是否包含关键字key是否在单链表当中boolean contains(E key);//删除第一次出现关键字为key的节点void remove(E key);//删除所有值为key的节点void removeAllKey(E key);//得到单链表的长度int size();void display();void clear();
}

二、MyLinkedList

package bagfive;/*** Created with IntelliJ IDEA.* Description:* User: tangyuxiu* Date: 2024-08-21* Time: 20:30*/
public class MyLinkedList<E> implements IList<E> {/* 使用内部类定义双向链表节点 */private static class ListNode<E> {E val;ListNode<E> prev;ListNode<E> next;public ListNode(E val) {this.val = val;}}private ListNode<E> head;private ListNode<E> last;@Overridepublic void addFirst(E data) {ListNode<E> newNode = new ListNode<>(data);//1. 如果链表为nullif (this.head == null) {this.head = this.last = newNode;return;}//2. 链表不为nullnewNode.next = this.head;this.head.prev = newNode;this.head = newNode;}@Overridepublic void addLast(E data) {ListNode<E> newNode = new ListNode<>(data);//1. 如果链表为nullif (this.head == null) {this.head = this.last = newNode;return;}//2. 链表不为nullthis.last.next = newNode;newNode.prev = this.last;this.last = newNode;}/* 判断add的位置是否合法 */private boolean addIndexIsLegal(int pos) {if (pos < 0 || pos > this.size()) {return false;}return true;}@Overridepublic void addIndex(int pos, E data) {//1. 判断pos位置是否合法if (!this.addIndexIsLegal(pos)) {return;}//2. pos == 0if (pos == 0) {this.addFirst(data);return;}//3. pos == size()if (pos == this.size()) {this.addLast(data);return;}//4. 其他位置,不需要找前驱节点了ListNode<E> newNode = new ListNode<>(data);ListNode<E> cur = this.head;for (int i = 0; i < pos; i++) {cur = cur.next;}newNode.next = cur;cur.prev.next = newNode;newNode.prev = cur.prev;cur.prev = newNode;}@Overridepublic boolean contains(E key) {ListNode<E> cur = this.head;while (cur != null) {if (cur.val.equals(key)) {return true;}cur = cur.next;}return false;}@Overridepublic void remove(E key) {// 头,任意,尾 (链表为null进不去循环)ListNode<E> del = this.head;while (del != null) {if (del.val.equals(key)) {if (this.head == del) {//如果为头节点if (this.head.next == null) {//如果为头节点,且链表只有一个节点this.head = this.last = null;} else {this.head = this.head.next;this.head.prev = null;}} else {if (this.last == del) {//如果为尾节点this.last = this.last.prev;this.last.next = null;} else {//其他位置del.prev.next = del.next;del.next.prev = del.prev;}}return;}del = del.next;}}@Overridepublic void removeAllKey(E key) {// 头,只一个节点,任意,尾 (链表为null进不去循环)ListNode<E> del = this.head;while (del != null) {if (del.val.equals(key)) {if (this.head == del) {//如果为头节点if (this.head.next == null) {//如果为头节点,且链表只有一个节点this.head = this.last = null;} else {this.head = this.head.next;this.head.prev = null;}} else {if (this.last == del) {//如果为尾节点this.last = this.last.prev;this.last.next = null;} else {del.prev.next = del.next;//其他位置del.next.prev = del.prev;}}}del = del.next;}}@Overridepublic int size() {int count = 0;ListNode<E> cur = this.head;while (cur != null) {count++;cur = cur.next;}return count;}@Overridepublic void display() {ListNode<E> cur = this.head;while (cur != null) {System.out.print(cur.val + " ");cur = cur.next;}System.out.println();}@Overridepublic void clear() {ListNode<E> cur = this.head;while (cur != null) {cur.val = null;cur = cur.next;}this.head = this.last = null;//🙀}
}
http://www.lryc.cn/news/429691.html

相关文章:

  • Go 语言协程管理精解
  • C库函数signal()信号处理
  • 007 SpringCloudAlibaba基础使用(nacos,gateway)
  • 编译环境揭秘
  • 不同的字符集(ASCII、UTF-8、UTF-16/UCS-2、UTF-32/UCS-4)
  • STM32f407 网络接收 fpga 的 bin 文件并更新到 fpga series7(3)
  • JavaScript基础知识(七)
  • 20240821让飞凌的OK3588-C的核心板在Linux R4下挂载1TB的exFAT格式的TF卡
  • Java HashMap练习
  • 前后端分离项目实战-通用管理系统搭建(前端Vue3+ElementPlus,后端Springboot+Mysql+Redis)第三篇:登录功能优化
  • 8.20 Redis ACL配置 多个用户连接同一个Redis
  • 【C语言】static和extern的作用
  • 全新分支版本!微软推出Windows 11 Canary Build 27686版
  • 【Linux】ARM服务器命令行安装虚拟机
  • Android 10.0 锁屏页面忘记锁屏密码情况下点击5次解锁图标弹出锁屏密码功能实现
  • Java-CompletableFuture工具类
  • C语言:递归
  • 自动化测试框架pytest+allure+requests
  • Python 笔记 numpy.ndarray切片
  • 一、HTML5知识点精讲
  • 【杂乱算法】前缀和与差分
  • Arduino调试ESP32常见问题 exit status 1
  • “决胜面试:高频题目与算法策略一览”
  • Node-RED的安装
  • java中的Collections
  • linux Qt QkeyEvent及驱动键盘按键捕获
  • 【GH】【EXCEL】P6: Shapes
  • google浏览器chrome用户数据(拓展程序,书签等)丢失问题
  • 数据结构——链式队列和循环队列
  • 数据库死锁解决方法,学费了吗?