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++;
        }
  • 相关阅读:
    jquery 实现弹出框 打开与关闭
    原生 将数组内容分别存入创建的循环单行栏(复选框+内容)里 并验证
    利用jquery 实现单页定位动画运动
    CSS样式 鼠标状态
    cookie 的使用:打开集团站自动跳转到当前城市所对应的网站,通过改变城市跳转到当前城市所对应的网站
    表单验证--通过原生js模仿ajax的异步交互
    springMVC之单文件上传
    springMVC之普通表单提交
    分页之页码数量显示
    cron表达式详解
  • 原文地址:https://www.cnblogs.com/lyajs/p/5973234.html
Copyright © 2011-2022 走看看