• 链表03--[双向链表&&单向循环链表&&双向循环链表&&静态链表]


    1.双向链表

    java官方就是双向链表

    1.1双向链表-只有一个元素

    1.2双向链表-node方法

    /**
         * 获取index位置对应的节点对象
         * @param index
         * @return
         */
        private Node<E> node(int index) {
            rangeCheck(index);
            
            if (index < (size >> 1)) {
                Node<E> node = first;
                for (int i = 0; i < index; i++) {
                    node = node.next;
                }
                return node;
            } else {
                Node<E> node = last;
                for (int i = size - 1; i > index; i--) {
                    node = node.prev;
                }
                return node;
            }
        }
    View Code

    1.3双向链表-add(int index,E element)

    @Override
        public void add(int index, E element) {
            rangeCheckForAdd(index);
    
            // size == 0
            // index == 0
            if (index == size) { // 往最后面添加元素
                Node<E> oldLast = last;
                last = new Node<>(oldLast, element, null);
                if (oldLast == null) { // 这是链表添加的第一个元素
                    first = last;
                } else {
                    oldLast.next = last;
                }
            } else {
                Node<E> next = node(index); 
                Node<E> prev = next.prev; 
                Node<E> node = new Node<>(prev, element, next);
                next.prev = node;
                
                if (prev == null) { // index == 0
                    first = node;
                } else {
                    prev.next = node;
                }
            }
            
            size++;
        }
    View Code

    1.4双向链表-删除操作clear()

    java如何删除对象

    1.看一个对象是否有引用

    2.看这个引用是否是gc.root对象

    2.1被栈(局部变量)指针指向的对象为gc.root对象

    @Override
        public void clear() {
            size = 0;
            first = null;
            last = null;
        }
    View Code

    1.5双向链表--remove(int index)

        @Override
        public E remove(int index) {
            rangeCheck(index);
    
            Node<E> node = node(index);
            Node<E> prev = node.prev;
            Node<E> next = node.next;
            
            if (prev == null) { // index == 0
                first = next;
            } else {
                prev.next = next;
            }
            
            if (next == null) { // index == size - 1
                last = prev;
            } else {
                next.prev = prev;
            }
            
            size--;
            return node.element;
        }
    View Code

    1.6双向链表VS单向链表

    1.7双向链表VS动态数组

    1.8LinkedList源码分析

     2.单向循环链表

     2.1单向循环链表--只有一个节点

     2.2单向循环链表--add(int index,E element)

        @Override
        public void add(int index, E element) {
            rangeCheckForAdd(index);
            
            if (index == 0) {
                Node<E> newFirst = new Node<>(element, first);
                // 拿到最后一个节点
                Node<E> last = (size == 0) ? newFirst : node(size - 1);
                last.next = newFirst;
                first = newFirst;
            } else {
                Node<E> prev = node(index - 1);
                prev.next = new Node<>(element, prev.next);
            }
            size++;
        }
    View Code

    2.3单向循环链表--remove(int index)

        @Override
        public E remove(int index) {
            rangeCheck(index);
            
            Node<E> node = first;
            if (index == 0) {
                if (size == 1) {
                    first = null;
                } else {
                    Node<E> last = node(size - 1);
                    first = first.next;
                    last.next = first;
                }
            } else {
                Node<E> prev = node(index - 1);
                node = prev.next;
                prev.next = node.next;
            }
            size--;
            return node.element;
        }
    View Code

    3.双向循环链表

     3.1双向循环链表--只有一个节点

     3.2双向循环链表--add(int index,E element)

        @Override
        public void add(int index, E element) {
            rangeCheckForAdd(index);
    
            // size == 0
            // index == 0
            if (index == size) { // 往最后面添加元素
                Node<E> oldLast = last;
                last = new Node<>(oldLast, element, first);
                if (oldLast == null) { // 这是链表添加的第一个元素
                    first = last;
                    first.next = first;
                    first.prev = first;
                } else {
                    oldLast.next = last;
                    first.prev = last;
                }
            } else {
                Node<E> next = node(index); 
                Node<E> prev = next.prev; 
                Node<E> node = new Node<>(prev, element, next);
                next.prev = node;
                prev.next = node;
                
                if (next == first) { // index == 0
                    first = node;
                }
            }
            
            size++;
        }
    View Code

    3.3双向循环链表--remove(int index)

    @Override
        public E remove(int index) {
            rangeCheck(index);
            return remove(node(index));
        }
        
        private E remove(Node<E> node) {
            if (size == 1) {
                first = null;
                last = null;
            } else {
                Node<E> prev = node.prev;
                Node<E> next = node.next;
                prev.next = next;
                next.prev = prev;
                
                if (node == first) { // index == 0
                    first = next;
                }
                
                if (node == last) { // index == size - 1
                    last = prev;
                }
            }
            
            size--;
            return node.element;
        }
    View Code

    4.静态链表

     5.ArrayList能否进一步优化

  • 相关阅读:
    Redis 安装和启动
    有序的map类--LinkedHashMap
    Springmvc
    工厂模式
    Top K
    判断链表是否有环
    注解
    Spring 事务
    热点账户高并发解决方案
    sql优化
  • 原文地址:https://www.cnblogs.com/ggnbnb/p/12179415.html
Copyright © 2020-2023  润新知