Java LinkedList class Source parsing

Source: Internet
Author: User

LinkedList bottom for doubly linked lists also inherit the Abstractsequentiallist<e>, and the ArrayList array compared to the reading efficiency is low, does not support random reading, fragmentation of space utilization is high, the average random insertion efficiency is relatively high. It can also be used to implement a queue. Properties are:

transient int size = 0;list size

transient node<e> first; head pointer

transient Node<e> last; tail hands

Private void Linkfirst (e E)

void linklast (e E)

Add e to the head and tail of the list, size and Modcount plus a

void Linkbefore (e E, node<e> succ) before inserting E into the SUCC node

Private E Unlinkfirst (node<e> f)

Private E Unlinklast (node<e> L)

Remove the head or tail node, size minus 1,modcount plus 1, the next Prev item value of the removed node is set to NULL to trigger the GC

E unlink (node<e> x) Remove the node x, you need to determine whether there is a precursor and a rear drive node, if not to change the tail pointer, the same will remove the node's next prev item value is set to NULL to trigger the GC

Public E GetFirst ()

Public E GetLast ()

Returns the node to which first or last points are thrown, and the list is empty.

Public E Removefirst ()

Public E Removelast ()

Call Unlinklast to remove and return to the head or tail of the node, the list is empty when thrown wrong

Public void AddFirst (e E) calls Linkfirst (e), inserting elements into the head

Public void addlast (e E)

Public Boolean Add (E E)

Two methods are called Linklast (e), except the return value is equivalent

Public When a boolean remove (Object o) is o==null, all elements of the x==null are removed by means of the Unlink method, otherwise the elements of the o.equals (x) are removed, Every element that has a match is called once unlink so the increment of Modcount is the number of removed elements

Public boolean addall (int index, collection<?) extends E> c) Inserts a collection from C into the index position. First check that index is in line with the list length range, if there is no element in C directly return false, otherwise traverse the elements in C to produce a new node and link to the index point position, check if you need to modify the first and last position, and finally modify size and modcount++

Public void Clear () iterates through all nodes and sets the last Prev item to Null,size to 0,modcount++.

Public E set (int index, e Element) is set to item=element after checking the index range and does not change modcount

Public void Add (int index, E Element) checks the index range, and if index==size is inserted and then ends, call Linklast (Element), Otherwise, calling Linkbefore (element, node (index)) will cause modcount++

Public E Remove (int index) checks the index range, index >= 0 && Index < size calls unlink (index) to remove the element, modcount++

node<e> node (int index) returns the node of index subscript, and if index is more than half of size, look for the head from last, otherwise search backwards from first

Public int indexOf (Object o) looks for the smallest list element with an O equal subscript, returns 1 if not, and the comparison logic is still based on whether O is null to differentiate

Public int lastIndexOf (Object o) searches for the first qualifying element subscript from the tail, similar to the previous method

Public E Peek () returns the item that first points to the node, or null if NULL

Public E element () is also the return First.item, the difference is that empty will throw the wrong

Public E poll () on the basis of peek (), the first element is deleted if not null

Public Boolean offer (e) with Add (e)

Public boolean Offerfirst (E-e) with AddFirst

Public boolean offerlast (E-e) with AddLast

Public E Peekfirst () with Peek ()

Public E Peeklast () returns the trailing element, or NULL when NULL is returned

Public E Pollfirst () with poll ()

Public E Polllast () returns the trailing element, empty returns NULL, no trailing element is removed for null

Public void push (e E) adds E to the head

Public E pop () Remove Head element

Public boolean removefirstoccurrence (Object o) with remove (O)

Public boolean removelastoccurrence (Object o) removes the last element equal to O

Public Object[] ToArray () Create a new array and iterate through the list to copy the elements into the array

Private void writeobject (java.io.ObjectOutputStream s)

Private void ReadObject (java.io.ObjectInputStream s)

Serialized in the same way as ArrayList, is done through the object input and output stream, the input is called Linklast will read the element to join the end of the list

Java LinkedList class Source parsing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.