当前位置: 首页 > news >正文

LinkedList源码

继承AbstractSequentialList 实现了顺序访问列表
实现List、Deque、Cloneable
List接口定义List集合的操作方法
Deque支持在两端插入和删除元素的线性集合。
Cloneable实现此接口的类可以进行拷贝操作
 
重要说明:
1、定了first头节点(first.pre=null && first.item != null)、last尾节点(last.next=null && last.item != null)
2、clear()清空链表,设置first=last=null,每个节点值设置为null,每个节点next以及每个节点pre设置为null,
3、Node<E> node(int index)获得index位置的节点Node,技巧:先判断index是在size/2之前还是之后,减少一半的查询次数
 
代码翻译:
public class LinkedList<E>
    extends AbstractSequentialList<E>
    implements List<E>, Deque<E>, Cloneable, java.io.Serializable
{
    链表长度
    transient int size = 0;

    定义头节点
    transient Node<E> first;

    定义尾节点
    transient Node<E> last;

    定义空的链表
    public LinkedList() {
    }

     构建包含指定集合c的链表
    public LinkedList(Collection<? extends E> c) {
        this();
        addAll(c);
    }

    将对象e作为链表头,根据对象e构建Node节点,e的pre设置为null,e的next设置为之前头节点f
    private void linkFirst(E e) {
        final Node<E> f = first;
        final Node<E> newNode = new Node<>(null, e, f);
        first = newNode;
        if (f == null)
            last = newNode;
        else
            f.prev = newNode;
        size++;
        modCount++;
    }

    将对象e作为链表结尾,根据对象e构建Node节点,e的pre设置为之前的结尾l节点,e的next设置为null
    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++;
    }

   将对象e设置为succ节点之前,根据对象e构建Node节点,e的pre设置为succ的pre节点,e的next设置为succ节点
    void linkBefore(E e, Node<E> succ) {
        // assert succ != null;
        final Node<E> pred = succ.prev;
        final Node<E> newNode = new Node<>(pred, e, succ);
        succ.prev = newNode;
        if (pred == null)
            first = newNode;
        else
            pred.next = newNode;
        size++;
        modCount++;
    }

    将头节点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;
    }

    将尾节点l删除掉
    private E unlinkLast(Node<E> l) {
        // assert l == last && l != null;
        final E element = l.item;
        final Node<E> 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;
    }

    将节点x删除掉
    E unlink(Node<E> x) {
        // assert x != null;
        final E element = x.item;
        final Node<E> next = x.next;
        final Node<E> prev = x.prev;

        if (prev == null) {
            first = next;
        } else {
            prev.next = next;
            x.prev = null;
        }

        if (next == null) {
            last = prev;
        } else {
            next.prev = prev;
            x.next = null;
        }

        x.item = null;
        size--;
        modCount++;
        return element;
    }

    获得头几点f的值
    public E getFirst() {
        final Node<E> f = first;
        if (f == null)
            throw new NoSuchElementException();
        return f.item;
    }

    获得尾节点的值
    public E getLast() {
        final Node<E> l = last;
        if (l == null)
            throw new NoSuchElementException();
        return l.item;
    }

    移除头节点
    public E removeFirst() {
        final Node<E> f = first;
        if (f == null)
            throw new NoSuchElementException();
        return unlinkFirst(f);
    }

    移除尾节点
    public E removeLast() {
        final Node<E> l = last;
        if (l == null)
            throw new NoSuchElementException();
        return unlinkLast(l);
    }

    添加头节点
    public void addFirst(E e) {
        linkFirst(e);
    }

    添加尾节点
    public void addLast(E e) {
        linkLast(e);
    }

    判断对象o在链表中是否存在
    public boolean contains(Object o) {
        return indexOf(o) != -1;
    }

    返回链表长度
    public int size() {
        return size;
    }

    在尾节点后添加对象e的节点
    public boolean add(E e) {
        linkLast(e);
        return true;
    }

    移除值为ode节点
    public boolean remove(Object o) {
        if (o == null) {
            for (Node<E> x = first; x != null; x = x.next) {
                if (x.item == null) {
                    unlink(x);
                    return true;
                }
            }
        } else {
            for (Node<E> x = first; x != null; x = x.next) {
                if (o.equals(x.item)) {
                    unlink(x);
                    return true;
                }
            }
        }
        return false;
    }

    将集合c添加到链表结尾
    public boolean addAll(Collection<? extends E> c) {
        return addAll(size, c);
    }

    将集合c添加到指定位置
    public boolean addAll(int index, Collection<? extends E> c) {
        checkPositionIndex(index);

        Object[] a = c.toArray();
        int numNew = a.length;
        if (numNew == 0)
            return false;

        Node<E> 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<E> newNode = new Node<>(pred, e, null);
            if (pred == null)
                first = newNode;
            else
                pred.next = newNode;
            pred = newNode;
        }

        if (succ == null) {
            last = pred;
        } else {
            pred.next = succ;
            succ.prev = pred;
        }

        size += numNew;
        modCount++;
        return true;
    }

    清空链表,设置first=last=null,值设置为null,next以及pre设置为null,
    public void clear() {
        // Clearing all of the links between nodes is "unnecessary", but:
        // - helps a generational GC if the discarded nodes inhabit
        //   more than one generation
        // - is sure to free memory even if there is a reachable Iterator
        for (Node<E> x = first; x != null; ) {
            Node<E> next = x.next;
            x.item = null;
            x.next = null;
            x.prev = null;
            x = next;
        }
        first = last = null;
        size = 0;
        modCount++;
    }


    获得指定位置index的值
    public E get(int index) {
        checkElementIndex(index);
        return node(index).item;
    }

    将指定位置index的值设置为element
    public E set(int index, E element) {
        checkElementIndex(index);
        Node<E> x = node(index);
        E oldVal = x.item;
        x.item = element;
        return oldVal;
    }

    在指定位置index增加节点e
    public void add(int index, E element) {
        checkPositionIndex(index);

        if (index == size)
            linkLast(element);
        else
            linkBefore(element, node(index));
    }

    删除指定位置的节点
    public E remove(int index) {
        checkElementIndex(index);
        return unlink(node(index));
    }

    判断index位置是否在链表中有值
    private boolean isElementIndex(int index) {
        return index >= 0 && index < size;
    }

    判断index位置是否在链表中
    private boolean isPositionIndex(int index) {
        return index >= 0 && index <= size;
    }

    检查index位置是否在链表中有值
    private void checkElementIndex(int index) {
        if (!isElementIndex(index))
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
    }
    
    判断index位置是否在链表中
    private void checkPositionIndex(int index) {
        if (!isPositionIndex(index))
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
    }

    获得index位置的节点Node,技巧:先判断index是在size/2之前还是之后,减少一个的查询次数
    Node<E> node(int index) {
        // assert isElementIndex(index);

        if (index < (size >> 1)) {
            Node<E> x = first;
            for (int i = 0; i < index; i++)
                x = x.next;
            return x;
        } else {
            Node<E> x = last;
            for (int i = size - 1; i > index; i--)
                x = x.prev;
            return x;
        }
    }

    // Search Operations

    按照从头到尾的顺序,获得对象o在链表中位置
    public int indexOf(Object o) {
        int index = 0;
        if (o == null) {
            for (Node<E> x = first; x != null; x = x.next) {
                if (x.item == null)
                    return index;
                index++;
            }
        } else {
            for (Node<E> x = first; x != null; x = x.next) {
                if (o.equals(x.item))
                    return index;
                index++;
            }
        }
        return -1;
    }

    按照从尾到头的顺序,获得对象o在链表中位置
    public int lastIndexOf(Object o) {
        int index = size;
        if (o == null) {
            for (Node<E> x = last; x != null; x = x.prev) {
                index--;
                if (x.item == null)
                    return index;
            }
        } else {
            for (Node<E> x = last; x != null; x = x.prev) {
                index--;
                if (o.equals(x.item))
                    return index;
            }
        }
        return -1;
    }

    // Queue operations.

    查看头节点的值
    public E peek() {
        final Node<E> f = first;
        return (f == null) ? null : f.item;
    }

    获得头节点的值,如果f=null,则报错
    public E element() {
        return getFirst();
    }

    获得头节点并且移除
    public E poll() {
        final Node<E> f = first;
        return (f == null) ? null : unlinkFirst(f);
    }

    删除头节点
    public E remove() {
        return removeFirst();
    }

    在尾节点添加对象e的节点
    public boolean offer(E e) {
        return add(e);
    }

    在头节点添加对象为e的头节点
    public boolean offerFirst(E e) {
        addFirst(e);
        return true;
    }

    在尾节点后添加对象e的节点
    public boolean offerLast(E e) {
        addLast(e);
        return true;
    }

    查看头节点的值
    public E peekFirst() {
        final Node<E> f = first;
        return (f == null) ? null : f.item;
     }

    查看尾节点的值
    public E peekLast() {
        final Node<E> l = last;
        return (l == null) ? null : l.item;
    }

    返回头节点并且删除
    public E pollFirst() {
        final Node<E> f = first;
        return (f == null) ? null : unlinkFirst(f);
    }

    返回尾节点并且删除
    public E pollLast() {
        final Node<E> l = last;
        return (l == null) ? null : unlinkLast(l);
    }

    添加在头节点添加对象为e的节点
    public void push(E e) {
        addFirst(e);
    }

    获得头节点并且删除,如果f=null,则报错
    public E pop() {
        return removeFirst();
    }

    按照从头到尾的顺序,删除对象o在链表中第一次出现的节点
    public boolean removeFirstOccurrence(Object o) {
        return remove(o);
    }

    按照从尾到头的顺序,删除对象o在链表中第一次出现的节点
    public boolean removeLastOccurrence(Object o) {
        if (o == null) {
            for (Node<E> x = last; x != null; x = x.prev) {
                if (x.item == null) {
                    unlink(x);
                    return true;
                }
            }
        } else {
            for (Node<E> x = last; x != null; x = x.prev) {
                if (o.equals(x.item)) {
                    unlink(x);
                    return true;
                }
            }
        }
        return false;
    }
}

 

转载于:https://www.cnblogs.com/use-D/p/9594958.html

相关文章:

  • 爬虫基础 - 抓包
  • Object.assign方法不能实现深复制
  • 手拉手教你实现一门编程语言 Enkel, 系列 13
  • css过渡,css动画,页面布局分析,表单元素
  • JSONModel使用
  • 9月10日科技联播:马云将回归教育事业,张勇接任阿里巴巴董事局主席
  • 中国人寿如何基于容器搭建金融PaaS云平台
  • Docker删除镜像
  • mysql 查询当天、本周,本月,上一个月的数据
  • java8之后的时间api
  • 站内全文搜索
  • 登录功能测试点
  • vue项目ide(vue项目环境搭建)
  • 逆地址解析协议RARP
  • linux之history命令
  • Android路由框架AnnoRouter:使用Java接口来定义路由跳转
  • HTTP中的ETag在移动客户端的应用
  • Java 11 发布计划来了,已确定 3个 新特性!!
  • Java多线程(4):使用线程池执行定时任务
  • JDK9: 集成 Jshell 和 Maven 项目.
  • Linux中的硬链接与软链接
  • Otto开发初探——微服务依赖管理新利器
  • Quartz初级教程
  • Spring框架之我见(三)——IOC、AOP
  • TypeScript迭代器
  • vue脚手架vue-cli
  • 基于游标的分页接口实现
  • 检测对象或数组
  • 简单基于spring的redis配置(单机和集群模式)
  • 如何优雅的使用vue+Dcloud(Hbuild)开发混合app
  • 通过来模仿稀土掘金个人页面的布局来学习使用CoordinatorLayout
  • 再谈express与koa的对比
  • 中文输入法与React文本输入框的问题与解决方案
  • 你对linux中grep命令知道多少?
  • 《码出高效》学习笔记与书中错误记录
  • python最赚钱的4个方向,你最心动的是哪个?
  • ​你们这样子,耽误我的工作进度怎么办?
  • ​人工智能之父图灵诞辰纪念日,一起来看最受读者欢迎的AI技术好书
  • (70min)字节暑假实习二面(已挂)
  • (二)丶RabbitMQ的六大核心
  • (附源码)python房屋租赁管理系统 毕业设计 745613
  • .a文件和.so文件
  • .Net Redis的秒杀Dome和异步执行
  • .NET开发不可不知、不可不用的辅助类(一)
  • /proc/stat文件详解(翻译)
  • @ConditionalOnProperty注解使用说明
  • @RequestBody的使用
  • [20190416]完善shared latch测试脚本2.txt
  • [Android开源]EasySharedPreferences:优雅的进行SharedPreferences数据存储操作
  • [BJDCTF 2020]easy_md5
  • [ChromeApp]指南!让你的谷歌浏览器好用十倍!
  • [go] 迭代器模式
  • [hadoop读书笔记] 第十五章 sqoop1.4.6小实验 - 将mysq数据导入HBASE
  • [leetcode]Search a 2D Matrix @ Python
  • [Linux] 常用命令--版本信息/关机重启/目录/文件操作