zoukankan      html  css  js  c++  java
  • LinkedList

    一、节点

       //节点的数据结构
        private static class Node<E> {
            E item; //元素
            Node<E> next; //后继
            Node<E> prev; //前驱
    
            Node(Node<E> prev, E element, Node<E> next) {
                this.item = element;
                this.next = next;
                this.prev = prev;
            }
        }
        

    二、构造函数

       //无参构造函数
        public LinkedList() {
        }
    
        //构造一个包含指定集合的元素的列表
        public LinkedList(Collection<? extends E> c) {
            this();
            addAll(c);
        }

    插入一个元素后,first和last都指向这个元素

    /**
         * index :Collection第一个元素插入的位置
         */
        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;
            //集合中没有一个元素,讲前驱节点指向last,后继为null
            if (index == size) {
                succ = null;
                pred = last;
            } else {
                //获取index位置的节点,后继节点为当前的节点,前驱节点为当前节点的前驱节点。
                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;
        }

    获取指定位置的节点

     /**
         * 如果index在集合的前半部分则从前边遍历,
         * 如果在集合的后半部分则从后边遍历。提高效率。
         */
        Node<E> node(int 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;
            }
        }

    将元素添加到首尾元素

        //将元素添加为首元素
        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++;
        }
    
        //将元素作为尾元素
        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++;
        }
  • 相关阅读:
    Win7下VS2010不能链接问题
    10种CSS3实现的Loading效果
    js之事件冒泡和事件捕获及其阻止详细介绍
    JavaScript事件冒泡与捕获
    VUE和ES6资源收集
    vuejs心法和技法
    使用CSS3开启GPU硬件加速提升网站动画渲染性能
    CSS Sprites(CSS图像拼合技术)教程、工具集合
    使用CSS为图片添加更多趣味的5种方法
    使用CSS完美实现垂直居中的方法
  • 原文地址:https://www.cnblogs.com/lyajs/p/5973234.html
Copyright © 2011-2022 走看看