博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java容器源码解析之——LinkedList
阅读量:4660 次
发布时间:2019-06-09

本文共 8862 字,大约阅读时间需要 29 分钟。

我们直接从源码来分析LinkedList的结构:

public class LinkedList
extends AbstractSequentialList
implements List
, Deque
, Cloneable, java.io.Serializable

LinkedList是List和Deque接口的双向链表的实现。实现了所有可选列表操作,并允许包括null值

LinkedList既然是通过双向链表去实现的,那么它可以被当作堆栈、队列或双端队列进行操作。并且其顺序访问非常高效,而随机访问效率比较低。

LinkedList节点结构:

private static class Node
{ E item; //节点元素 Node
next; //指向下一个节点位置 Node
prev; //指向前一个节点位置 Node(Node
prev, E element, Node
next) { this.item = element; this.next = next; this.prev = prev; } }

LinkedList涉及的一些操作链表的变量:

transient int size = 0; //链表中元素的个数transient Node
first; //指向链表的头节点transient Node
last; //指向链表尾节点
transient 关键字修饰变量表示该值不参与序列化

 

LinkedList构造函数:

第二种构造函数过程比较复杂,这里重点讲解

1.调用默认构造函数

2.调用addAll(c)方法,最终调用的是addAll(size, c)方法

  (1)该方法先进行index越界判断,由于index = size所以没有越界

  (2)将集合c转换成Object数组,判断数组长度是否为0,为0返回false

  (3) 设置Node<E> pred, succ指针辅助链表操作

    由于index == size,所以设置succ = null, pred = last,相当于从链表尾进行插入操作

  (4)遍历Object数组,根据数组每个元素生成一个Node节点,并且修改指针将节点插入链表

  下面假设Object有3个元素,具体展示下3个节点的插入过程:

初始Node
pred, succ; 两个指针为空,且first,last也为空   然后执行:
    if (index == size) { //当我们插入位置为链表节点的最后位置时            succ = null;            pred = last;      }

  接着执行:

for (Object o : a) {            @SuppressWarnings("unchecked") E e = (E) o;            Node
newNode = new Node<>(pred, e, null); if (pred == null) //pred为空,说明该链表为空,所以设置newNode为第一个节点 first = newNode; // first指向第一个节点 else pred.next = newNode; pred = newNode; }
  if (succ == null) {            last = pred;        }
 

 

    

下面是具体的源码:

public LinkedList() { //默认构造函数    }  public LinkedList(Collection
c) { //传入集合参数的构造函数 this(); //调用默认构造函数 addAll(c); //方法见下面 } public boolean addAll(Collection
c) { //回调addAll(size, c) 是重载方法 return addAll(size, c); //size值为当前节点个数 } public boolean addAll(int index, Collection
c) { //该方法实现再链表index位置插入集合中的所有元素 checkPositionIndex(index); //检查index是否越界 Object[] a = c.toArray();  //将集合c转换成Object数组 int numNew = a.length;  //数组长度 if (numNew == 0)  //判断数组长度,为0直接返回false return false;    //下面将构建链表 Node
pred, succ;   //辅助指针 if (index == size) {
//当我们插入位置为链表节点的最后位置时 succ = null; pred = last; } else { succ = node(index); pred = succ.prev; } for (Object o : a) { @SuppressWarnings("unchecked") E e = (E) o; Node
newNode = new Node<>(pred, e, null); if (pred == null) //pred为空,说明该链表为空,所以设置newNode为第一个节点 first = newNode; // first指向第一个节点 else pred.next = newNode; pred = newNode; } if (succ == null) { last = pred; } else { pred.next = succ; succ.prev = pred; } size += numNew; //元素个数增加 modCount++;  //链表修改次数加一 return true; } private void checkPositionIndex(int index) {
//检查index是否越界 if (!isPositionIndex(index)) throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); }
private boolean isPositionIndex(int index) { //判断index值        return index >= 0 && index <= size;     }

 

public boolean addAll(int index, Collection
c)方法还有一种情况,是在index下插入元素,该index != size 我们还是从源码分析: 这里执行else操作, succ = node(index)具体解析在源码注释中体现 具体的节点插入就不再多讲,大家画过图能分析清楚的。
Node
pred, succ;   //辅助指针 if (index == size) { //当我们插入位置为链表节点的最后位置时 succ = null; pred = last; } else { succ = node(index); //succ指向index节点的后一个位置 pred = succ.prev; //pred执行index节点位置 } Node
node(int index) { // assert isElementIndex(index); if (index < (size >> 1)) { //判断index是否小于size/2,如果小于从前往后查找,如果大于从后往前查找 Node
x = first; for (int i = 0; i < index; i++) x = x.next; return x; } else { Node
x = last; for (int i = size - 1; i > index; i--) x = x.prev; return x; } }

 Deque双端链表操作:

  插入:

public void addFirst(E e) { //将指定元素插入此列表的开头        linkFirst(e);    }  private void linkFirst(E e) { //插入列表开头的具体方法        final Node
f = first; //定义一个f指向first队头指针 final Node
newNode = new Node<>(null, e, f); //新建一个newNode,该Node下一个指针指向f(即原列表头位置) first = newNode; //fisrt指向newNode节点,也就是指向现在的列表第一个节点 if (f == null) //如果原链表尾空 last = newNode; //原链表last也必须指向newNode else //链表不为空 f.prev = newNode; //设置原链表头节点的prev 指向newNode size++; //链表元素个数加一 modCount++; //链表修改次数加一 } public void addLast(E e) { //将指定元素添加到此列表的结尾 linkLast(e); } void linkLast(E e) { final Node
l = last; //定义一个l指向链表最后一个节点位置即last指向的位置 final Node
newNode = new Node<>(l, e, null);//新建一个newNode,该Node的prev执行原链表的最后一个节点 last = newNode;//原链表的last指针指向newNode if (l == null) //如果原链表为空,设置原链表头节点指向newNode first = newNode; else //如果原链表不为空,设置原链表最后一个节点的next指向newNode l.next = newNode; size++; modCount++; } public boolean offerFirst(E e) { //在此列表的开头插入指定的元素 addFirst(e); return true; } public boolean offerLast(E e) { //在此列表末尾插入指定的元素 addLast(e); return true; }

  删除:

public E removeFirst() { //移除并返回此列表的第一个元素        final Node
f = first; if (f == null) throw new NoSuchElementException(); return unlinkFirst(f); //删除头节点 } public E removeLast() { //移除并返回此列表的最后一个元素 final Node
l = last; if (l == null) throw new NoSuchElementException(); return unlinkLast(l); } public E pollFirst() { final Node
f = first; return (f == null) ? null : unlinkFirst(f); } private E unlinkFirst(Node
f) { // assert f == first && f != null; final E element = f.item; //设置一个Element存储要删除节点的值,作为返回值 final Node
next = f.next;// 定义一个next指向第二个节点 f.item = null; //将头节点值设置为空 f.next = null; // help GC 断开与头节点的next指针 first = next; //将头指针指向next节点的位置 if (next == null) //如果原链表只有一个元素,删除后,需要将last指向null last = null; else next.prev = null; size--; modCount++; return element; } public E pollLast() { final Node
l = last; return (l == null) ? null : unlinkLast(l); }
private E unlinkLast(Node
l) { //删除链尾元素 // assert l == last && l != null; final E element = l.item; final Node
prev = l.prev; l.item = null; l.prev = null; // help GC last = prev; if (prev == null) //当删除该元素后链表为空,头指针设置为空 first = null; else prev.next = null; size--; modCount++; return element; }

栈操作:

public void push(E e) { //入栈,操作列表头        addFirst(e);    }public E pop() { //出栈        return removeFirst();    } public E peek() {
//获取头节点元素 final Node
f = first; return (f == null) ? null : f.item; }

队列:

public boolean offer(E e) { //队尾入队        return add(e);    }public boolean add(E e) {        linkLast(e);        return true;    }   void linkLast(E e) { //列表尾添加元素        final Node
l = last; final Node
newNode = new Node<>(l, e, null); last = newNode; if (l == null) first = newNode; else l.next = newNode; size++; modCount++; } public E poll() { //队头出队 final Node
f = first; return (f == null) ? null : unlinkFirst(f); } private E unlinkFirst(Node
f) {
//删除列表头元素 // assert f == first && f != null; final E element = f.item; final Node
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; } public E peek() { //查看队头元素 final Node
f = first; return (f == null) ? null : f.item; }

 

LinkedList总体介绍都这里了,都是居于链表操作,其他的一些方法大家可以结合源码分析下。

  

 

 

 

转载于:https://www.cnblogs.com/huangyichun/p/6549688.html

你可能感兴趣的文章
ffmpeg
查看>>
vue和angular的区别
查看>>
PowerDNS简单教程(4):优化篇
查看>>
React 介绍
查看>>
常用判断重复记录的SQL语句
查看>>
一些比较常用的在Markdown使用的数学符号
查看>>
周一干不干活-PHP+MySQLi
查看>>
ThinkPHP5 与 composer
查看>>
js 将json字符串转换为json对象的方法解析
查看>>
编程技巧总结
查看>>
北漂随笔 01
查看>>
[小知识点]绝对路径与相对路径.
查看>>
C#_DataGridView之导出到Excel
查看>>
selenium 代码常见报错
查看>>
fatal error LNK1103: debugging information corrupt; recompile module 解决
查看>>
在不兼容IE中推动发展
查看>>
Too many arguments provided to function-like macro invocation 或
查看>>
代码大全读后感(三)
查看>>
Java中23种设计模式详解
查看>>
Codeforces 988F Rain and Umbrellas 【dp】
查看>>