• 【Java】LinkedList模拟实现


    整体框架

    在这里插入图片描述

    IMyLinkedList接口

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

    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();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    IndexNotLegalException异常类

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

    public class IndexNotLegalException extends RuntimeException{
        public IndexNotLegalException() {
        }
    
        public IndexNotLegalException(String msg) {
            super(msg);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    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;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    addFirst(头插)

    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;
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    addLast(尾插)

    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;
            }
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    在指定位置插入数据

    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不合法...");
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    判断是否存在

    public boolean contains(int key) {
            ListNode cur = head;
            while(cur != null){
                if(cur.val == key){
                    return true;
                }
                cur = cur.next;
            }
            return false;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    移除第一个相等的节点

    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;
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    移除所有相等的节点

    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;
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    链表的长度

     public int size() {
            int count = 0;
            ListNode cur = head;
            while (cur != null) {
                count++;
                cur = cur.next;
            }
            return count;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    打印链表

        public void display() {
            ListNode cur = head;
            while (cur != null) {
                System.out.print(cur.val + " ");
                cur = cur.next;
            }
            System.out.println();
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    释放回收链表

    public void clear() {
            ListNode cur = head;
            while(cur != null){
                ListNode curN = cur.next;
                cur.prev = null;
                cur.next = null;
                cur = curN;
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
  • 相关阅读:
    浅述mesh广播中的1827与1828
    Django Swagger文档库drf-spectacular
    static关键字的注意事项
    自从新来了个字节20K出来的,就见识到了什么是天花板
    Chapter11 : Deep Learning in Structure-Based Drug Design
    苹果手机相册怎么全部删除照片?这样做快人一步
    【力扣每日一题】2023.10.11 奖励最顶尖的k名学生
    02-Gin请求参数
    ysoserial CommonsCollections3 分析
    Linux|shell编程|拷贝大文件之显示进度条
  • 原文地址:https://blog.csdn.net/2202_75560428/article/details/137154576