LinkedList底层结构和源码分析

LinkedList全面说明

  1. LinkedList底层实现了双向链表和双端队列特点
  2. 可以添加任意元素(元素可以重复),包括null
  3. 线程不安全,没有实现同步

LinkedList的底层操作机制

  1. LinkedList底层维护了一个双向链表.
  2. LinkedList中维护了两个属性first和last分别指向首节点和尾节点
  3. 每个节点(Node对象),里面又维护了prev、next、item三个属性,其中通过prev指向前一个,通过next指向后一个节点。最终实现双向链表.
  4. 所以LinkedList的元素的添加和删除,不是通过数组完成的,相对来说效率较高。

模拟一个简单的双向链表

package com.Collection_.List_;

/**
 * @author pengyang
 * @date 2022-06-09 09:27
 */

public class LinkedList01 {
    public static void main(String[] args) {
        //模拟一个简单的双向链表
        Node jack = new Node("jack");
        Node tom = new Node("tom");
        Node hsp = new Node("hsp");

        //连接三个节点,形成双向链表
        jack.next = tom;
        tom.next = hsp;
        hsp.pre = tom;
        tom.pre = jack;

        Node first = jack;//双向链表的头节点
        Node last = hsp;//双向链表的尾节点

        //从头到尾遍历
        System.out.println("---------从头到尾遍历--------");
        while (first != null) {
            System.out.println(first);
            first = first.next;
        }

        //从尾到头遍历
        System.out.println("---------从头到尾遍历--------");
        while (last != null) {
            System.out.println(last);
            last = last.pre;
        }

        //链表添加数据/对象
        Node smith = new Node("smith");

        smith.next = hsp;
        smith.pre = tom;
        tom.next = smith;
        hsp.pre = tom;

        first = jack;//双向链表的头节点
        //从头到尾遍历
        System.out.println("---------从头到尾遍历--------");
        while (first != null) {
            System.out.println(first);
            first = first.next;
        }
    }
}

class Node {
    public Object item;
    public Node next;
    public Node pre;

    public Node(Object item) {
        this.item = item;
    }

    @Override
    public String toString() {
        return "Node{" +
                "item=" + item +
                '}';
    }
}

LinkedList 源码

package com.Collection_.List_;

import java.util.LinkedList;

/**
 * @author pengyang
 * @date 2022-06-10 13:12
 */

public class LinkedListCRUD {
    public static void main(String[] args) {

        LinkedList linkedList = new LinkedList();
        linkedList.add(1);
        linkedList.add(2);
        linkedList.add(3);

        linkedList.remove();    //这里默认删除第一个节点

        System.out.println("linkedList" + linkedList);

        /*
        //添加
        1.LinkedList linkedList = new LinkedList();
        此时linkeList 的属性 first = null last = null

        2.执行添加
        public boolean add(E e) {
            linkLast(e);
        return true;
        }

        3.将新的节点,加入到双向链表最后
        void linkLast(E e) {
            final Node<E> l = last;
            final Node<E> newNode = new Node<>(l, e, null);
            last = newNode;
            if (l == null)
                first = newNode;
            else
                l.next = newNode;
            size++;
            modCount++;
        }

        //删除
        linkedList.remove();
        1.执行
        public E remove() {
            return removeFirst();
        }

        2.执行
        public E removeFirst() {
            final Node<E> f = first;
            if (f == null)
                throw new NoSuchElementException();
            return unlinkFirst(f);
        }

        3.执行
        private E unlinkFirst(Node<E> f) {
            // assert f == first && f != null;
            final E element = f.item;
            final Node<E> next = f.next;
            f.item = null;
            f.next = null; // help GC
            first = next;
            if (next == null)
                last = null;
            else
                next.prev = null;
            size--;
            modCount++;
            return element;
        }

         */

    }
}

ArrayList和LinkedList的比较

在这里插入图片描述
如何选择ArrayList和LinkedList:

  1. 如果我们改查的操作多,选择ArrayList
  2. 如果我们增删的操作多,选择LinkedList
  3. 一般来说,在程序中,80%-90%都是查询,因此大部分情况下会选择ArrayList
  4. 在一个项目中,根据业务灵活选择,也可能这样,一个模块使用的是ArrayList,另外一个模块是LinkedList,也就是说,要根据业务来进行选择
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值