• java - 数据结构,双向链表 - LinkedList


    一、双向链表 (不带头)

    无头双向链表:在Java的集合框架库中LinkedList底层实现就是无头双向循环链表
    在这里插入图片描述

    • 双向链表 和 单向链表的区别,就在于 双向 比 单向 多个 一个前驱地址。而且 你会发现 正因为有了前驱地址,所以所以这个链表,它有两种走向,这也是这个链表为什么叫做双向链表的原因之一

    首先看看单链表是如何删除节点的

    在这里插入图片描述
    总结:
       单向链表在删除一个节点的时候,需要借助前驱节点,才能删除。

    双向链表是如何删除节点的

    双向链表删除节点,不需要借助前驱节点
    因为双向链表,它存储前驱和后驱的节点的地址,它都知道。
    另外双向链表会多一个引用last,这个引用永远指向此时链表的尾巴节点
    head就是永远指向双向链表的头节点。

    在这里插入图片描述

    二、模拟实现双向链表

    2.1、实现一个类,来表示双向链表的节点

    class ListNode{
        //存储int类型的数据
        public int val;
        //存储上一个节点的地址
        public ListNode prev;
        //存储下一个节点的地址
        public ListNode next;
        
        public ListNode(int val){
            //构造方法
            this.val = val;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    2.2、实现一个类,来表示双向链表

    public class MyLinkedList {
        //指向双向链表的头结点
        public ListNode head;
        //指向双向链表的尾巴节点
        public ListNode last;
    
        //头插法
        public void addFirst(int data){
            
        };
        //尾插法
        public void addLast(int data){
            
        };
        //任意位置插入,第一个数据节点为0号下标
        public boolean addIndex(int index,int data){
            return true;
        };
        //查找是否包含关键字key是否在单链表当中
        public boolean contains(int key){
            return true;
        };
        //删除第一次出现关键字为key的节点
        public void remove(int key){
            
        };
        //删除所有值为key的节点
        public void removeAllKey(int key){
            
        };
        //得到单链表的长度
        public int size(){
            return 0;
        };
        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
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    实现LinkedList中的所有方法
    通过这些方法就可以操作链表

    打印链表

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

    得到链表的长度

    //得到链表的长度
        public int size(){
            ListNode cur = this.head;
            int count = 0;
            while(cur != null){
                count++;
                cur = cur.next;
            }
           return count;
        };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    查找是否包含关键字key是否在单链表当中

    //查找是否包含关键字key是否在单链表当中
    //找到返回true,找不到返回false
        public boolean contains(int key){
            ListNode cur = this.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
    • 11
    • 12

    头插法

    //头插法
        public void addFirst(int data){
            ListNode node = new ListNode(data);
            if(this.head == null){
                this.head = node;
                this.last = node;
            }else {
                node.prev = this.head.prev;
                node.next = head;
                head = node;
            }
        };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    在这里插入图片描述

    尾插法

    //尾插法
        public void addLast(int data){
            ListNode node = new ListNode(data);
            if(this.head == null){
                this.head = node;
                this.last = node;
            } else {
                this.last.next = node;
                node.prev = this.last;
                last = node;
           
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    在这里插入图片描述

    任意位置插入,第一个数据节点为0号下标

    //任意位置插入,第一个数据节点为0号下标
        //任意位置插入,第一个数据节点为0号下标
        public void addIndex(int index,int data){
            ListNode node = new ListNode(data);
            //当链表为空的时候
            if(this.head == null){
                addFirst(data);
                return;
            }
            //当链表不为空的时候,
            if(index == 0){
                addFirst(data);
                return;
            }
            if (index == size()){
                addLast(data);
                return;
            }
            ListNode cur = this.head;
            while(index-1 != 0){
                cur  = cur.next;
                index--;
            }
            node.prev = cur;
            node.next = cur.next;
            cur.next = node;
            node.prev = cur.next.prev;
    
        };
    
    • 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

    在这里插入图片描述

    删除第一次出现关键字为key的节点

    //删除第一次出现关键字为key的节点
        public void remove(int key){
            ListNode cur = this.head;
            if(this.head == null){
                System.out.println("链表为空");
                return;
            }
            //如果删除的是头结点
            if(this.head.val == key){
                this.head = this.head.next;
                this.head.prev = null;
                return;
            }
            //如果删除的是尾巴结点
            if(this.last.val == key){
                this.last = this.last.prev;
                this.last.next = null;
                return;
            }
            //删除的是中间的节点
            while (cur != null){
                if(cur.val == key){
                    cur.prev.next = cur.next;
                    cur.next.prev = cur.prev;
                    return ;
                }
                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
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    删除所有值为key的节点

        //删除所有值为key的节点
        public void removeAllKey(int key){
            if(this.head == null){
                System.out.println("链表为空!");
                return;
            }
            ListNode cur = this.head;
            while(cur != null){
                if(cur.val == key){
                    if(cur == this.head){
                        //如果key值是头结点
                        this.head = this.head.next;
                        if(head != null){
                            //不止一个节点
                            this.head.prev = null;
                        }else {
                            //如果只有一个节点,并且也是要删除的节点
                            this.last = null;
                        }
                    }else {
                        cur.prev.next = cur.next;
                        //不是头结点
                       if(cur.next != null){
                           cur.next.prev = cur.prev;
                       }else {
                           this.last = this.last.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
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    清空链表里面的元素

    public void clear(){
            if(this.head == null){
                System.out.println("链表为空!");
                return;
            }
    
            while (this.head != null){
                ListNode headNext = this.head.next;
                this.head.prev =null;
                this.head.next = null;
                this.head = headNext;// head 在此过程中,置为null,因为 最后一个元素的next 等null,
            }
            this.last = null;
        };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    总结 - 模拟实现双向链表

    class ListNode{
        //存储int类型的数据
        public int val;
        //存储上一个节点的地址
        public ListNode prev;
        //存储下一个节点的地址
        public ListNode next;
    
        public ListNode(int val){
            //构造方法
            this.val = val;
        }
    }
    
    public class MyLinkedList {
        //指向双向链表的头结点
        public ListNode head;
        //指向双向链表的尾巴节点
        public ListNode last;
    
    
        //得到链表的长度
        public int size(){
            ListNode cur = this.head;
            int count = 0;
            while(cur != null){
                count++;
                cur = cur.next;
            }
           return count;
        };
        
        //打印链表
        public void display(){
            ListNode cur = this.head;
            while(cur != null){
                System.out.print(cur.val + " ");
                cur = cur.next;
            }
            System.out.println();
        };
    
        //头插法
        public void addFirst(int data){
            ListNode node = new ListNode(data);
            if(this.head == null){
                this.head = node;
                this.last = node;
            }else {
                node.prev = this.head.prev;
                node.next = head;
                this.head = node;
            }
        };
        
        //尾插法
        public void addLast(int data){
            ListNode node = new ListNode(data);
            if(this.head == null){
                this.head = node;
                this.last = node;
            } else {
                this.last.next = node;
                node.prev = this.last;
                last = node;
            }
        };
        
        //任意位置插入,第一个数据节点为0号下标
        public void addIndex(int index,int data){
            ListNode node = new ListNode(data);
            //当链表为空的时候
            if(this.head == null){
                addFirst(data);
                return;
            }
            //当链表不为空的时候,
            if(index == 0){
                addFirst(data);
                return;
            }
            if (index == size()){
                addLast(data);
                return;
            }
            ListNode cur = this.head;
            while(index-1 != 0){
                cur  = cur.next;
                index--;
            }
            node.prev = cur;
            node.next = cur.next;
            cur.next = node;
            node.prev = cur.next.prev;
    
        };
    
        //查找是否包含关键字key是否在单链表当中
        public boolean contains(int key){
            ListNode cur = this.head;
            while (cur != null){
                if(cur.val == key){
                    return true;
                }
                cur = cur.next;
            }
            return false;
        };
    
        //删除第一次出现关键字为key的节点
        public void remove(int key){
            ListNode cur = this.head;
            if(this.head == null){
                System.out.println("链表为空");
                return;
    		  }
    
            //如果删除的是头结点
            if(this.head.val == key){
                this.head = this.head.next;
                //this.head.prev = null;
                return;
            }
            //如果删除的是尾巴结点
            if(this.last.val == key){
                this.last = this.last.prev;
                this.last.next = null;
                return;
            }
            //删除的是中间的节点
            while (cur != null){
                if(cur.val == key){
                    cur.prev.next = cur.next;
                    cur.next.prev = cur.prev;
                    return;
                }
                cur = cur.next;
            }
    
        };
    
        //删除所有值为key的节点
        public void removeAllKey(int key){
            if(this.head == null){
                System.out.println("链表为空!");
                return;
            }
            ListNode cur = this.head;
            while(cur != null){
                if(cur.val == key){
                    if(cur == this.head){
                        //如果key值是头结点
                        this.head = this.head.next;
                        if(head != null){
                            //不止一个节点
                            this.head.prev = null;
                        }else {
                            //如果只有一个节点,并且也是要删除的节点
                            this.last = null;
                        }
                    }else {
                        cur.prev.next = cur.next;
                        //不是头结点
                       if(cur.next != null){
                           cur.next.prev = cur.prev;
                       }else {
                           this.last = this.last.prev;
                       }
                    }
                }
                cur = cur.next;
            }
        };
    
      
    
    
        public void clear(){
            if(this.head == null){
                System.out.println("链表为空!");
                return;
            }
    
            while (this.head != null){
                ListNode headNext = this.head.next;
                this.head.prev =null;
                this.head.next = null;
                this.head = headNext;// head 在此过程中,置为null,因为 最后一个元素的next 等null,
            }
            this.last = null;
        };
    }
    
    
    • 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
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193

    三、顺序表和链表的区别和联系

    组织数据上:

    • 1、顺序表底层是一个数组,他是一个逻辑上和物理上都是连续的
    • 2、链表是一个由若干节点组成的一个数据结构,逻辑上是连续的但是在物理上【内存上】是不连续的。

    操作数据上:

    • 1、顺序表适合,查找相关的操作,因为,可以使用下标,直接获取到某个位置的元素。
    • 2、链表适合于,频繁的插入和删除操作。此时不需要像顺序表一样,移动元素。链表的插入只需要修改指向即可。
    • 3、顺序表还有不好的地方,就是你需要看满不满,满了要扩容,扩容了之后,不一定都能放完。所以,他的空间上的利用率不高。
  • 相关阅读:
    如何在interface中处理DUT中的inout信号
    join、inner join、left join、right join、outer join的区别
    银行业生产系统存储数据迁移方法及实践
    处理验证码和登录页面
    ywtool network命令
    LeetCode·每日一题·952.按公因数计算最大组件大小·并查集
    openwrt RK3568_EVB移植
    LeetCode 2810.故障键盘
    2028全球子宫肌瘤栓塞剂行业调研及趋势分析报告
    scratch还原轨迹 2023年5月中国电子学会图形化编程 少儿编程 scratch编程等级考试四级真题和答案解析
  • 原文地址:https://blog.csdn.net/ryy1999/article/details/128157305