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

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

【难点】:remove、removeAllKey

一、IList

package bagfour;/*** Created with IntelliJ IDEA.* Description:* User: tangyuxiu* Date: 2024-08-20* Time: 20:58*/
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 clear();void display();
}

二、MyLinkedList

package bagfour;/*** Created with IntelliJ IDEA.* Description:* User: tangyuxiu* Date: 2024-08-20* Time: 20:58*/
public class MyLinkedList<E> implements IList<E> {/* 使用内部类来定义链表节点 */private static class ListNode<E> {E val;ListNode<E> next;//默认为nullpublic ListNode(E val) {this.val = val;}}private ListNode<E> head;/* 头插 */@Overridepublic void addFirst(E data) {ListNode<E> newNode = new ListNode<>(data);this.head.next = this.head;this.head = newNode;}/* 尾插 */@Overridepublic void addLast(E data) {ListNode<E> newNode = new ListNode<>(data);//1. 如果链表为nullif (this.head == null) {this.head = newNode;return;}//2. 尾插ListNode<E> cur = this.head;while (cur.next != null) {cur = cur.next;}cur.next = newNode;}/* 判断add位置是否合法 */private boolean addIndexIsLegal(int pos) {if (pos < 0 || pos > this.size()) {return false;}return true;//如果链表为null,且pos位置为0,此时也是合法的}/* 任意位置插入 */@Overridepublic void addIndex(int pos, E data) {//1. 判断add位置是否合法if (!this.addIndexIsLegal(pos)) {return;}//2. pos == 0(链表为null且index=0,也走的这里)if (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;//寻找pos节点的前一个节点//【思路】本来想要找到到index下标所指向的节点的,但发现// 我们要找的其实不是index下标所指向的节点而是要找到它的前一个节点// 那么我们将cur原本要走的index步,改为走index - 1步即可for (int i = 0; i < pos - 1; i++) {//这里的问题头插部分已经考虑到了🙀cur = cur.next;}newNode.next = cur.next;cur.next = 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;}/* 删除第一次出现关键字为key的节点 */@Overridepublic void remove(E key) {//1. 如果链表为nullif (this.head == null) {return;}//2. 如果key在头节点处if (this.head.val.equals(key)) {this.head = this.head.next;return;}//3. 如果key在其他位置ListNode<E> pre = this.head;//找到key的前一个节点while (pre.next != null) {ListNode<E> del = pre.next;if (del.val.equals(key)) {pre.next = del.next;return;}pre = pre.next;}}/* 删除所有值为key的节点 */@Overridepublic void removeAllKey(E key) {//1. 如果链表为nullif (this.head == null) {return;}//2. 其他位置ListNode<E> pre = this.head;ListNode<E> del = pre.next;while (pre.next != null) {if (del.val.equals(key)) {pre.next = del.next;//del = del.next;} else {pre = pre.next;//del = del.next;}del = del.next;}//3. 如果key在头节点处if (this.head.val.equals(key)) {//为什么下行代码不写成pre = pre.next呢?//答:“正常”的代码中pre,find只是工具,真正的head一直仍然指向的是同一个节点this.head = this.head.next;}}/* 得到单链表的长度 */@Overridepublic int size() {int count = 0;ListNode<E> cur = this.head;while (cur != null) {count++;cur = cur.next;}return count;}/* 清空链表 */@Overridepublic void clear() {ListNode<E> cur = this.head;while (cur != null) {cur.val = null;cur = cur.next;}this.head = null;}/* 打印 */@Overridepublic void display() {ListNode<E> cur = this.head;while (cur != null) {System.out.print(cur.val + " ");cur = cur.next;}System.out.println();}
}

http://www.mrgr.cn/news/8386.html

相关文章:

  • github访问加速项目@一键部署自动更改host修改加速Github访问
  • 12、stm32通过dht11读取温湿度
  • chromedriver下载地址大全(包括124.*后)以及替换exe后仍显示版本不匹配的问题
  • RK3588J正式发布Ubuntu桌面系统,丝滑又便捷!
  • CmoS相关概念
  • 【jetson交叉编译(6)】orin ubuntu的库安装,通过apt下载deb 库,然后的解压到具体位置/opt/test/3rd
  • 前端数据存在什么地方,刷新页面之后依旧存在
  • 零基础5分钟上手亚马逊云科技-搭建CDN加速应用访问
  • Spring Boot结合RabbitMQ使用总结
  • K8S 无状态应用有状态应用
  • 游戏开发设计模式之组件模式
  • Java 面向对象的三大特性和五大基本原则
  • 系统架构不是设计出来的
  • git的讲解
  • 设计模式-备忘录模式
  • Docker
  • AI数字时代客户体验白皮书5G云算力网络云网终端AIGC人工智能宽带政企物联网专线 IDC智慧城市专家学者教授培训讲师分享
  • Adobe After Effects的插件--------CC Ball Action
  • Apache Spark 的基本概念和在大数据分析中的应用。
  • pycharm 隐藏 __ init __ .py 文件