【Java】LinkedList模拟实现

目录

整体框架

IMyLinkedList接口

这个接口用来存放所有方法,之后用MyLinkedList来实现这个接口,重写里面的方法

java 复制代码
public interface IMyLinkedList {
    //头插法
    public void addFirst(int data);
    //尾插法
    public void addLast(int data);
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data);
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key);
    //删除第一次出现关键字为key的节点
    public void remove(int key);
    //删除所有值为key的节点
    public void removeAllKey(int key);
    //得到链表的长度
    public int size();
    //打印链表
    public void display();
    //释放链表
    public void clear();
}

IndexNotLegalException异常类

IndexNotLegalExceprion异常类用来判断index是否合法

java 复制代码
public class IndexNotLegalException extends RuntimeException{
    public IndexNotLegalException() {
    }

    public IndexNotLegalException(String msg) {
        super(msg);
    }
}

MyLinkedList类

成员变量(节点信息)

java 复制代码
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;

addFirst(头插)

java 复制代码
public void addFirst(int data) {
        ListNode newNode = new ListNode(data);
        if(head == null){
            head = last = newNode;
        }else{
            newNode.next = head;
            head.prev = newNode;
            head = newNode;
        }
    }

addLast(尾插)

java 复制代码
public void addLast(int data) {
        ListNode newNode = new ListNode(data);
        if(head == null){
            head = last = newNode;
        }else{
            newNode.prev = last;
            last.next = newNode;
            last = newNode;
        }

    }

在指定位置插入数据

java 复制代码
public void addIndex(int index, int data) {
        //判断index的合法性
        try{
            checkIndex(index);
        }catch(IndexNotLegalException e){
            e.printStackTrace();
        }
        if(index == 0){
            addFirst(data);
            return;
        }
        if(index == size()){
            addLast(data);
            return;
        }
        ListNode newNode = new ListNode(data);
        ListNode cur = head;
        while(index != 0){
            cur = cur.next;
            index--;
        }
        newNode.next = cur;
        newNode.prev = cur.prev;
        cur.prev.next = newNode;
        cur.prev = newNode;
    }
    private void checkIndex(int index){
        if(index<0 || index>size()){
            throw new IndexNotLegalException("Index不合法...");
        }
    }

判断是否存在

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

移除第一个相等的节点

java 复制代码
public void remove(int key) {
        ListNode cur = head;
        if(head.val == key && head == last){
            head = last = null;
            return;
        }
        //如果删除的在head节点
        if(head.val == key){
            head = head.next;
            head.prev = null;
            return;
        }
        while(cur != null){
            if(cur.val == key){
                cur.prev.next = cur.next;
                if(cur.next == null){
                    last = cur.prev;
                    break;
                }
                cur.next.prev = cur.prev;
            }
            cur = cur.next;
        }
    }

移除所有相等的节点

java 复制代码
public void removeAllKey(int key) {
        ListNode cur = head;
        if(head.val == key && head == last){
            head = last = null;
            return;
        }
        //如果删除的在head节点
        if(head.val == key){
            head = head.next;
            head.prev = null;
        }
        while(cur != null){
            if(cur.val == key){
                cur.prev.next = cur.next;
                if(cur.next == null){
                    last = cur.prev;
                    break;
                }
                cur.next.prev = cur.prev;
            }
            cur = cur.next;
        }
    }

链表的长度

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

打印链表

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

释放回收链表

java 复制代码
public void clear() {
        ListNode cur = head;
        while(cur != null){
            ListNode curN = cur.next;
            cur.prev = null;
            cur.next = null;
            cur = curN;
        }
    }
相关推荐
砚底藏山河1 分钟前
【量化纯GET实战 #23】多股票相关性:用收益率看板块联动
java·数据库·python·金融·数据分析
抠脚小弟16 分钟前
Spring Cache 使用详解:从入门到实战
java·spring
ZGG00327 分钟前
MySQL 索引详解:B+ 树、聚簇索引与最左前缀
java·数据库·mysql
quantdash_cc28 分钟前
量化数据源怎么选:稳定性、实时性和数据完整性到底该如何权衡?
开发语言·python·数据分析·量化交易·股票数据·quantdash
萧瑟余晖36 分钟前
Java深入解析篇五十四之对象模型详解
java·开发语言
张文是假的啊1 小时前
Java | record | Controller逻辑
java·开发语言
勿忘,瞬间1 小时前
Mybatis高阶
java·数据库·mybatis
不甘先生1 小时前
Go 中 type、方法与指针接收者:从 str_name.Name() 看懂 Go 的类型系统
开发语言·后端·golang
quantdash_cc2 小时前
Python 股票 K 线数据质量校验:字段、缺失值、重复行和价格异常
开发语言·python·数据分析·量化交易·股票数据·quantdash
Zenova EdgeOS2 小时前
工业网关重试机制:从固定间隔到指数退避的工程实战
开发语言·网络·php