JAVA8 LinkedList

package link_list.src;

public class TestOne {

    /**
     * 
     * 1.无参构造方法和有参构造方法
     * 
     * 2. public boolean add(E e) {} 方法
     * 
     * 3.public E remove(int index) {}  删除指定位置的节点元素
     * 
     * 
     */

    
    
    /*
     * 2.
     * 对last操作,添加新节点的时候,把last作为pre,并对last重新复制,这样就形成了双向链表
     * 
     * 
    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++;
    }
    */
    
/*    
 * 根据索引获取节点的值
 * 
    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;
        }
    }
    
        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;
    }
    
    *
    *把前一个节点的next指向当前节点的next
    *把当前节点的next的prev指向当前节点的prev
    *
    *把当前节点的prev,next,item置空
    *
    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;
    }
    
    
    */
}

 

posted @ 2018-06-29 08:21  aLa神灯  阅读(280)  评论(0编辑  收藏  举报