博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LinkedList源码分析
阅读量:4710 次
发布时间:2019-06-10

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

1、成员变量

transient int size = 0;transient Node
first; // 头节点transient Node
last; // 尾节点

2、add()方法

/** * 将指定元素追加到list末尾 */public boolean add(E e) {    linkLast(e);    return true;}/** * 在指定位置插入元素 */public void add(int index, E element) {    checkPositionIndex(index);    if (index == size)        linkLast(element); // 插入到尾部    else        // 插入到指定位置,并将原位置的节点移动到下一个位置        linkBefore(element, node(index));}/** * 将元素作为最后一个节点插入链表 */void linkLast(E e) {    final Node
l = last; // 当前尾节点 final Node
newNode = new Node<>(l, e, null); last = newNode; if (l == null) // 没有尾节点(list为空),将头节点也设置为newNode first = newNode; else l.next = newNode; // 当前尾节点的下一个节点设置为newNode size++; modCount++;}/** * 在succ元素之前插入一个节点 */void linkBefore(E e, Node
succ) { // assert succ != null; final Node
pred = succ.prev; // succ的前一个节点 final Node
newNode = new Node<>(pred, e, succ); succ.prev = newNode; if (pred == null) // 没有前一个节点,表示succ已经是头节点 first = newNode; else pred.next = newNode; // succ前节点的next赋值为newNode size++; modCount++;}

检查索引是否合法:

private void checkPositionIndex(int index) {    if (!isPositionIndex(index))        throw new IndexOutOfBoundsException(outOfBoundsMsg(index));}private boolean isPositionIndex(int index) {    // 检查索引是否合法    return index >= 0 && index <= size;}

3、get()方法

public E get(int index) {    // 检查索引是否合法    checkElementIndex(index);    return node(index).item;}Node
node(int index) { // assert isElementIndex(index); if (index < (size >> 1)) { // index小于size的一半,说明index在list前半部分,从头节点开始遍历 Node
x = first; for (int i = 0; i < index; i++) x = x.next; return x; } else { // index大于等于size的一半,说明index在list后半部分,从尾节点开始遍历 Node
x = last; for (int i = size - 1; i > index; i--) x = x.prev; return x; }}

4、set()方法

public E set(int index, E element) {    // 检查索引是否合法    checkElementIndex(index);    Node
x = node(index); // 根据index找到目标节点 E oldVal = x.item; x.item = element; // 将目标节点的item替换为element return oldVal; // 返回原来的值}

5、remove()方法

/** * 移除第一个与指定元素相等的节点 */public boolean remove(Object o) {    if (o == null) { // 如果指定的节点值是null,移除list中第一个null节点        for (Node
x = first; x != null; x = x.next) { if (x.item == null) { unlink(x); return true; } } } else { // 使用equals方法找到指定节点 for (Node
x = first; x != null; x = x.next) { if (o.equals(x.item)) { unlink(x); return true; } } } return false;}/** * 根据位置移除节点 */public E remove(int index) { checkElementIndex(index); return unlink(node(index));}/** * 移除一个非空的节点 x */E unlink(Node
x) { // assert x != null; final E element = x.item; final Node
next = x.next; final Node
prev = x.prev; if (prev == null) { // x为头结点 first = next; } else { prev.next = next; // x的前后节点直接相连 x.prev = null; // 断开X与前节点的连接 } if (next == null) { // x为尾结点 last = prev; } else { next.prev = prev; // x的前后节点直接相连 x.next = null; // 断开X与后节点的连接 } x.item = null; // 清空x的数据存储 size--; modCount++; return element;}

未完,待续...

转载于:https://www.cnblogs.com/liycode/p/9330317.html

你可能感兴趣的文章
greendroid cast problem
查看>>
学习:Android框架
查看>>
学习IOS开发UI篇--数据存储
查看>>
Python3标准库
查看>>
BAT美团滴滴java面试大纲(带答案版)之四:多线程Lock
查看>>
jdk动态代理
查看>>
TeeChart经验总结 11.Tools
查看>>
[学习笔记] 多项式全家桶系列
查看>>
NOIP2015 运输计划
查看>>
c语言博客作业--结构体&文件
查看>>
PHP常用函数
查看>>
查询出总数集合
查看>>
【Quick 3.3】资源脚本加密及热更新(二)资源加密
查看>>
拦截导弹
查看>>
C#及时释放代码
查看>>
科技部:中国131家独角兽企业 名单文字版
查看>>
eclipse上安装 windowBuilder方法
查看>>
织梦dede:channel取子栏目时重复显示同级栏目的解决方法
查看>>
热修复 RocooFix篇(一)
查看>>
如何向Maven仓库(私服)中上传第三方jar包
查看>>