"JDK source code to read three" --LinkedList doubly linked list Detailed

Briefly describe the data structure and characteristics of LinkedList:

ArrayList and LinkedList implement the List interface, the LinkedList bottom is doubly linked list , so the index does not exist,

Query: LinkedList need to traverse all the nodes query from the list head or tail of the list, so the query is slow ,

When deleting: LinkedList only need to change the pointer, and the node to be removed is set to null, can, without changing the position of the element, so delete faster (FIG all the addresses indicating the address of the node).

1. LinkedList take a look at what are the attributes:

   transient  int size = 0 ; the number of nodes stored //

     transient the Node <E> First; // pointer to specify the first node

     transient the Node <E> Last; // pointer to specify the last node

  The Node Class LinkedList Node:

Private  static  class the Node <E> { 
        E Item; refers to the address of an object // store 
        the Node <E> Next; // points to the next node in the list of references to address 
        the Node <E> PREV; // points to a linked list on the reference node address of
 
        the node (the node <E> PREV, E Element, the node <E> Next) {
             the this .Item = Element;
             the this .next = Next;
             the this .prev = PREV; 
        } 
    }

2.LinkedList constructor:

  / ** 
     * Create an empty List 
     * / 
    public the LinkedList () { 
    } 

    / ** 
     * set in the order of construction of the iterator returned list specifies a collection of elements comprises. * / 
    Public LinkedList (Collection <? The extends E> c) {
         the this (); // first call the constructor with no arguments, create an empty list 
        addAll (c); // then call addAll () method, the elements in the collection in order, one by one insert the list 
    }

3. Some of the key methods

Node <E> Node (int index) the index node Node Get

  /**
     * Returns the (non-null) Node at the specified element index.
     */
    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;
        }
    }

This approach explains why LinkedList query slow because the list is not all indexes, only the head node and tail node query elements to determine the index in the entire list is left or right, traverse find, then return

private void linkFirst (E e) element as the first element of the list

    /**
     * Links e as first element.
     */
    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) The element as the last element of the list

    void linkLast (E E) {
         Final l = the Node <E> Last; when l // see as described by capital L Replace
         Final the Node <E> = the newNode new new the Node <> (l, E, null ); 
        Last = the newNode;
         IF (L == null ) 
            First = the newNode;
         the else 
            l.next = the newNode; 
        size ++ ; 
        ModCount ++ ; 
    }

These two methods are similar to linkLast (E e) as an example to explain the method, the new node L Last point, expressed as linked lists original end node, the new node the newNode, into the end of the list, which is a pointer to L prev, Next pointer to null, a new end node represents

L is determined whether the original end node is null, if the description is empty, uninitialized node list head, this time as the head node newNode, if not empty, the next pointer original end nodes, points to the new node newNode.

The unlink E (the Node <E> X) to cancel the non-empty node link

 1     /**
 2      * Unlinks non-null node x.
 3      */
 4     E unlink(Node<E> x) {
 5         // assert x != null;
 6         final E element = x.item;
 7         final Node<E> next = x.next;
 8         final Node<E> prev = x.prev;
 9       
10         if (prev == null) {
11             first = next;
12         } else {
13             prev.next = next;
14             x.prev = null;
15         }
16 
17         if (next == null) {
18             last = prev;
19         } else {
20             next.prev = prev;
21             x.next = null;
22         }
23 
24         x.item = null;
25         size--;
26         modCount++;
27         return element;
28     }
First get the current node on a node prev and the next node next, 
  if one node prev is empty, indicating that the current node is the first node, you need to lower the current node of a node next to the head node.
  Otherwise, the current node prev next pointer of a node, point to the next node of the current node;
  If the next node next empty, indicating that the current node is a terminal node, is necessary to a node of the current node prev is set to the end node. 
  Otherwise, the prev pointer to the next node next current node, point to a current node ;
then, the node element is set to null, waiting for the garbage collector.

Boolean the addAll public (int index, Collection <? the extends E> C) to set all the elements inserted into the specified position in the list, and displayed in the order returned by the iterator

 1   /**
 2      * Inserts all of the elements in the specified collection into this
 3      * list, starting at the specified position.  Shifts the element
 4      * currently at that position (if any) and any subsequent elements to
 5      * the right (increases their indices).  The new elements will appear
 6      * in the list in the order that they are returned by the
 7      * specified collection's iterator.
 8      *
 9      * @param index index at which to insert the first element
10      *              from the specified collection
11      * @param c collection containing elements to be added to this list
12      * @return {@code true} if this list changed as a result of the call
13      * @throws IndexOutOfBoundsException {@inheritDoc}
14      * @throws NullPointerException if the specified collection is null
15      */
16     public boolean addAll(int index, Collection<? extends E> c) {
17         checkPositionIndex(index); // 索引越界验证
18 
19         Object[] a =c.toArray ();
 20 is          int numNew = a.length;
 21 is          IF (numNew == 0 )
 22 is              return  to false ;
 23 is  
24          the Node <E> Pred, succ;
 25          IF (index == size) {// at the end node after inserting the element
 26 is              succ = null ;
 27              Pred = last;
 28          } the else {
 29              succ = node (index);
 30              Pred = succ.prev; // get node on a specified position of the node
 31          }
 32 
33 is          for (O Object: A) {// set all the elements traverse,
 34 is              @SuppressWarnings ( "an unchecked") E = E (E) O;
 35              the Node <E> = the newNode new new the Node <> (Pred, E, null );
 36              IF (Pred == null ) // list described list is empty, the head node disposed
 37 [                  First = the newNode;
 38 is              the else 
39                  pred.next = the newNode; // pointer to the next current node at a node index , point to the new node
 40              Pred = the newNode;
 41 is          }
 42 is  
43 is          IF (succ == null) {// If the end node is inserted, then set the end node
 44 is              Last = Pred;
 45          } the else {
 46 is              pred.next = succ; // new node inserted to complete the original node and the last index associating
 47              succ.prev = Pred;
 48          }
 49  
50          size = + numNew;
 51 is          ModCount ++ ;
 52 is          return  to true ;
 53 is      }
 

Additions and deletions had been saying LinkedList fast, slow queries, this method can know, in fact, more precisely, in the end increases faster when the element, when inserted into the specified location, or the need to traverse the query, as slow speed.

 

 

Guess you like

Origin www.cnblogs.com/Deters/p/11304596.html