LinkedList详解

LinkedList详解

基本介绍

  • LinkedList 底层实现了双向链表双端队列的特点

  • 可以添加任意元素(元素可以重复),包括null

  • 线程不安全,没有实现同步

LinkedList 的底层操作机制

  • LinkedList 底层维护了一个双向链表

  • LinkedList 中维护了两个属性 first 和 last 分别指向 首节点和尾节点

  • 每个节点(Node对象),里面又维护了prev、next、item三个属性,其中通过prev指向前一个节点,通过next指向后一个节点。最终实现双向链表。

  • item 用来存放数据

  • 所以LinkedList的元素的添加和删除,不是通过数组来完成的,相对来说效率较高

模拟一个简单的双向链表

public class Demo05 {
    public static void main(String[] args) {
        Node jack = new Node("jack");
        Node tom = new Node("tom");
        Node jay = new Node("jay");
​
        jack.next = tom;
        tom.next = jay;
        jay.pre = tom;
        tom.pre = jack;
​
        Node first = jack;
        Node last = jay;
​
        System.out.println("===从头到尾进行遍历===");
        while (true) {
            if (first == null) {
                break;
            }
            System.out.println(first);
            first = first.next;
        }
        
        System.out.println("===从尾到头进行遍历===");
        while (true) {
            if (last == null) {
                break;
            }
            System.out.println(last);
            last = last.pre;
        }
        
        //在tom和jay之间插入一个 smith
        Node smith = new Node("smith");
        smith.next = jay;
        smith.pre = tom;
        jay.pre = smith;
        tom.next = smith;
​
        first = jack; //让first再次指向jack
        System.out.println("===从头到尾进行遍历===");
        while (true) {
            if (first == null) {
                break;
            }
            System.out.println(first);
            first = first.next;
        }
​
    }
}
//定义一个Node类,Node对象表示双向链表的一个节点
class Node{
    public Object item; //存放数据
    public Node next;
    public Node pre;
​
    public Node(Object name) {
        this.item = name;
    }
​
    public String toString() {
        return "Node name" +item;
    }
}

LinkedList 的源码解读

public class Demo06 {
    public static void main(String[] args) {
        LinkedList linkedList = new LinkedList();
        linkedList.add(1);
        linkedList.add(2);
        linkedList.add(3);
        System.out.println(linkedList);
        
        linkedList.remove(); //默认删除的是链表的第一个节点
        System.out.println(linkedList);
        
        linkedList.set(1,999); //修改某个节点对象的数据
        System.out.println(linkedList);
        
        Object o = linkedList.get(1);//得到某个节点对象
        System.out.println(o);
       
        System.out.println("===LinkedList迭代器遍历===");
        Iterator iterator = linkedList.iterator();
        while (iterator.hasNext()) {
            Object next =  iterator.next();
            System.out.println("next="+next);
        }
        
        System.out.println("===LinkedList增强for循环遍历===");
        for (Object o1 : linkedList) {
            System.out.println("o1="+o1);
        }
        
        System.out.println("===LinkedList普通for循环遍历===");
        for (int i = 0; i <linkedList.size() ; i++) {
            System.out.println(linkedList.get(i));
        }
    }
}



  • 1.执行 LinkedList linkedList = new LinkedList();

    • 这时 LinkedList的属性 first = null、last = null

public LinkedList() {
}
  • 2.执行 linkedList.add(1);

public boolean add(E e) {
    linkLast(e);
    return true;
}
  • 3.执行 linkLast(e);

    • 将新的节点,加入到双向链表的最后,尾插法

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++;
}
  • 4.执行 linkedList.add(2);

public boolean add(E e) {
    linkLast(e);
    return true;
}
  • 5.执行 linkLast(e);

    • 将新的节点,加入到双向链表的最后,尾插法

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++;
}
  • 6.执行 linkedList.add(3); 同上



  • 7.执行 linkedList.remove();

    • 默认删除的是链表的第一个节点

public E remove() {
    return removeFirst();
}
  • 8.执行 return removeFirst();

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

    • 将 f 指向的双向链表的第一个节点删掉

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;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值