Tips:
1. In the reading process, we will only pick the simplest reading, such as order: Add (E), remove (Object o) If there are multiple overloaded methods. We'll pick the simplest one first.
2. Reading order, first need to see in front of the title there is a 1-the logo, after reading and then from the top down to add in the
[TOCM] from the perspective of structure
top-level interface
Serializable:
The serialized interface has no methods or fields and is used only to identify the semantics of serializable
Iterable:
Implementing this interface allows the object to be the target of the "foreach" statement.
Cloneable:
This class implements the Cloneable interface to indicate that the Object.clone () method can legitimately replicate the class instance by field. Implementation/Inheritance
Abstractsequentiallist:
This class provides the backbone implementation of the List interface, minimizing the effort required to implement this interface supported by a continuous access data store, such as a linked list. For random access data (such as arrays), abstractlist should be preferred instead of first using this class.
Deque:
A linear collection that supports inserting and removing elements at both ends. The name deque is the abbreviation for double ended queue (double-ended queues), which is usually read as "deck". Most Deque implementations have no fixed limit on the number of elements they can contain, but this interface supports both a capacity-constrained double-ended queue and a double-ended queue without a fixed size limit. from function reading-simple function start-list API
Generally in use is the most commonly used is, data storage, take, delete, loop traversal, from these points analysis source 1-boolean Add (e E)
Adds the specified element to the end of this list. This method is equivalent to AddLast (E).
In the Add method to use the Node class, you need to look at the following definition of the class, using the class to implement the linked list data structure
Public boolean Add (E e) {
linklast (e);
return true;
}
/**
* Links e as last element.
*
/void Linklast (E e) {
//Get the last element of this queue first
final node<e> l = previous;
Construction node, the previous node of this node is: The last node, the next node: Because the element is currently appended to the end of the list, the next node is null
final node<e> newNode = new node<> (l, E, NULL);
last = NewNode; Change the global variable so that the last node equals the new node
if (L = = null)
first = NewNode;/////The last node is NULL then the node is the current node, assuming it is the first time to be used: firstly and last are null; Add a new node, last and first is the node that is currently added
else
l.next = newNode;//If the last node has a value, the next node of the final node is the current node, like a chain, The new nodes are linked up
size++;
modcount++; It's a fast-failing version number
}
1-node-linked list implementation node class
The definition is simple.
private static class Node<e> {
E item; Current element
node<e> next;//Next element
node<e> prev;//previous Element
node (node<e> prev, E Element, node <E> next) {
This.item = element;
This.next = Next;
This.prev = prev;
}
}
void Add (int index, E Element)
Inserts the specified element in the location specified in this list.
Summary Random write, that is, the use of subscript, all the loop linked list to find the corresponding list node found node, write quickly, (do not need to move to the array of n-element subscript)
Inserts an element at the specified position, if the position already has an element, change the former/post index of this element public
void Add (int index, E element) {
Checkpositionindex (index); Also check if
(index = = size)//If the subscript equals the size of this list, it is equivalent to adding element linklast to the end of the list
;
else
Linkbefore (element, node (index));
}
/**
* Inserts element e before Non-null Node succ.
*
//Put the occupied node back in the
void Linkbefore (e E, node<e> succ) {
//assert SUCC! = null;
Take a closer look at the diagram behind the core function E unlink (node<e> x)
//Change the affected: Prev.next and Next.prev and first (why not last? Because added to the end of the list goes the normal add process)
final node<e> pred = Succ.prev;
Final node<e> NewNode = new Node<> (pred, E, succ);
Succ.prev = NewNode;
if (pred = = null) First
= NewNode;
else
pred.next = NewNode;
size++;
modcount++;
}
1-boolean Remove (Object o)
Removes the first occurrence of the specified element (if present) from this list.
Summary: Deleting the specified element, which is also required to traverse the matching ArrayList and LinkedList from the beginning, is done by looping through the first occurrence of the value;
Difference:
ArrayList Delete element needs to delete all elements behind the element move subscript LinkedList directly changes the affected front and back (end) of the Prev/next point, the deletion is completed, relatively, and the affected element is at most only (first/last variable, The first/last element of the deleted element), which is relatively higher than the list deletion performance,
public boolean remove (Object o) {
if (o = = null) {for
(node<e> x = first; X! = null; x = x.next) {
if ( X.item = = null) {
unlink (x);
return true;}}
} else {
//to traverse the list from the beginning until the next element is NULL indicates that the list is at the end of the for
(node<e> x = first; x = null; x = x.next) {
//Find matches to the specified element Linked list node
if (O.equals (X.item)) {
unlink (x);//Link list: Because a node is deleted, the front and back nodes of the node need to be linked up to
return true;}
}
}
return false;
}
1-core function-E unlink (Node x)
/**
* unlinks non-null node x.
* Disconnect the current element, remove the specified element from the list, and handle the broken link operation *
/E unlink (node<e> x) {
//assert x! = NULL; X is definitely not equal to NULL. Remember that this is a list node, not an element value, so it is never null (need to be judged externally); The original implementation can be defined as the
final E element = X.item;//Get element value
final node<e> Next = X.next; The next element
final node<e> prev = X.prev;////The
following logic beginner's words (such as me) can not understand, difficult to understand what is the logic, directly see the following diagram link delete element, you understand
if (prev = = null) {//The previous node is null, generally present, the current node is first first
= next;
} else {
prev.next = next;
X.prev = null;
}
if (next = null) {//The next node is null, which is generally: the current node is the last node, finally
= prev;
} else {
next.prev = prev;
X.next = null;
}
X.item = null; Manually set to NULL,GC
size--;
modcount++;
return element;
}
E Remove (int index)
Removes the element at the specified position in this list.
Summary: the linked list is treated as subscript, but it needs to be looped to determine the linked list node for index-I think it's ingenious. In the loop process, a small (partial) binary search is used to optimize the same meaning of the removal method, relative to ArrayList , LinkedList's random access is weaker than ArrayList (because ArrayList is positioned directly by subscript)
Delete an element by a specified index//list There is
no subscript, but there is always the number of elements, but also orderly, from the beginning to the tail, the equivalent is subscript the public
E Remove (int index) {
//check subscript is out of bounds
Checkelementindex (index);
This calls the core function, disconnects this element in the link, but first finds the corresponding list node according to the subscript
return unlink (Node (index));
}
private void Checkelementindex (int index) {
if (!iselementindex (index))
throw new Indexoutofboundsexception (outofboundsmsg (index));
}
/**
* Tells if the argument is the index of a existing element.
*
/private Boolean iselementindex (int index) {
//Here check subscript out of bounds more perfect, ArrayList inside not check whether it is negative, do not know why
Return index >= 0 && index < size;
}
core Function-node node (int index)
/** returns the corresponding list node based on the specified subscript
Returns the (non-null) node at the specified element index.
* As long as the subscript passed the check, then must be able to return a list node
*
/node<e> node (int index) {
//This is the assertion of the sentence, in the development of the time should be open, write the tool class, Once the test is online, the statement is commented out. It should be like this.
//Assert Iselementindex (index);
The following lookup also optimizes a little, not in any case, to look from scratch
//a bit similar to the partial binary lookup method, first two minutes, and then decide whether to start from the beginning or the tail
if (Index < (size >> 1)) {
node<e> x = First;
Traverse from the beginning, when i = index, then also found the subscript corresponding to the linked list node 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;
}
}
set-Replaces the element at the specified position in this list with the specified element.
Summary: in the LinkedList by subscript operation, you need to traverse the linked list to get the corresponding list node
Public E Set (int index, E element) {
Checkelementindex (index);//Check subscript
node<e> x = Node (index);//Find the index corresponding to the Linked list node
E oldval = X.item;
X.item = element;
return oldval;
}
E Get (int index)
Returns the element at the specified position in this list.
Summary:
1. Also called the Node (index) loop traversal to find the corresponding linked list node
Public E get (int index) {
checkelementindex (index);
Use the core function to find the corresponding linked list node
return node (index). Item;
}
Iterator Iterator ()
Transpose the method of the parent class, this is more abstract, I do not analyze.
Public iterator<e> Iterator () {return listiterator ();//abstractsequentiallist method of Parent class} =======
================== The following is Abstractlist method implementation ========================= public listiterator<e> ListIterator () {
Return Listiterator (0);
} public listiterator<e> listiterator (final int index) {rangecheckforadd (index);
return new Listitr (index); } private class Listitr extends Itr implements listiterator<e> {listitr (int index) {curso
R = Index;
} public boolean hasprevious () {return cursor! = 0;
} public E Previous () {checkforcomodification ();
try {int i = cursor-1;
E previous = Get (i);
Lastret = cursor = i;
return previous;
} catch (Indexoutofboundsexception e) {checkforcomodification (); throw new NosucheleMentexception ();
}} public int nextindex () {return cursor;
} public int Previousindex () {return cursor-1;
} public void set (E e) {if (Lastret < 0) throw new IllegalStateException ();
Checkforcomodification ();
try {AbstractList.this.set (Lastret, E);
Expectedmodcount = Modcount;
} catch (Indexoutofboundsexception ex) {throw new concurrentmodificationexception ();
}} public void Add (e e) {checkforcomodification ();
try {int i = cursor;
AbstractList.this.add (i, E);
Lastret =-1;
cursor = i + 1;
Expectedmodcount = Modcount;
} catch (Indexoutofboundsexception ex) {throw new concurrentmodificationexception ();
} }
} Boolean contains (Object o)
Returns true if this list contains the specified element.
Public Boolean contains (Object o) {
return indexOf (o)! =-1;
}
Similar to ArrayList's method of the same name. Are all traversed by the lookup public
int indexOf (Object o) {
int index = 0;
if (o = = null) {for
(node<e> x = first; X! = null; x = x.next) {
if (X.item = = null)
return INDEX;
index++;
}
} else {for
(node<e> x = first; X! = null; x = x.next) {
if (o.equals (X.item))
return index;
index++;
}
}
return-1;
}
Deque API Reading 1-void push (e E)
Pushes an element into the stack represented by this list. In other words, insert the element at the beginning of this list.
Summary: implementation is simple
1. Directly modify first and the affected nodes to point to
public void push (E e) {
addfirst (e);//Direct transpose AddFirst
}//
inserts the specified element at the beginning of this list. Public
void AddFirst (e e) {
linkfirst (e);
}
/**
* Links e as first element. Link to elements *
/private void Linkfirst (E e) {
final node<e> f = first;
final node<e> newNode = new node<> (null, E, f);
first = NewNode;
if (f = = null)//Only the first node will be NULL when the data is added for the first time, so the last node is the current node, which is
NewNode;
else
f.prev = NewNode;
size++;
modcount++;
}
1-e pop ()
POPs an element at the stack represented by this list. In other words, remove and return the first element of this list.
Public E pop () {
return Removefirst ();
}
Public E Removefirst () {
final node<e> f = first;
if (f = = null)
throw new Nosuchelementexception ();
Return Unlinkfirst (f);
}
1-core function-E Unlinkfirst (Node f)
/**
* unlinks non-null First node F.
* This function is similar to the unlink function, except that the unlink processing is more comprehensive, the function is only for first processing
*
/private E Unlinkfirst (node<e> f) {
//Assert F = = First && f! = null;
Final E element = F.item;
Final node<e> next = F.next;
F.item = null;
F.next = null; The Help GC pops up the first element, which contains the next element, which is also set to NULL,GC to ensure that first
= next is recycled;
if (next = = null) last
= null;
else
next.prev = null;//Because it is first so the previous one is null
size--;
modcount++;
return element;
}
1-E poll ()
Gets and removes the header of this list (first element)
Public E Poll () {
final node<e> f = first;
return (f = = null)? Null:unlinkfirst (f);
}
1-e Remove ()
Gets and removes the header of this list (the first element).
Public E Remove () {
return Removefirst ();
}
Public E Removefirst () {
final node<e> f = first;
if (f = = null)
throw new Nosuchelementexception ();
Return Unlinkfirst (f);
}
Summary of differences between poll and pop and remove
function is to remove first and return the removed element, the only difference is poll if first is NULL then return null pop throws an exception remove is also thrown exception 1-e peek ()
Gets the header (first element) of this list without removing it.
Public E Peek () {
final node<e> f = first;
return (f = = null)? Null:f.item;
}
1-e Element ()
Gets the header (first element) of this list without removing it.
Public E Element () {
return GetFirst ();
}
Public E GetFirst () {
final node<e> f = first;
if (f = = null)
throw new Nosuchelementexception ();
return f.item;
}
Summary: The difference between peek and element
Two functions are the same, the only difference is: element if the first node is null throws an exception 1-boolean offer (e e)
Adds the specified element to the end of this list (the last element).
Public A Boolean offer (e) {
//the API that directly entrusts list is added to the end of the list, that is, directly to the last point of change Operation
return Add (e);
}
Test Class Code
public class Linkedarraylisttest {linkedlist<string> data;
linkedlist<string> dequedata = new linkedlist<> ();
@Before public void before () {data = new linkedlist<> ();
Data.add ("1");
Data.add ("2");
Data.add ("3");
Data.add (NULL);
Data.add ("4");
Dequedata.push ("1");
Dequedata.push ("2");
Dequedata.push ("3");
Dequedata.push (NULL);
Dequedata.push ("4");
} @Test public void Add () {Data.add (1, "2");
} @Test public void Remove () {data.remove (null);
Data.remove ("1");
Data.remove (1);
} @Test public void set () {data.set (0, "1000");
} @Test public void get () {String s = data.get (2);
} @Test public void foreach () {iterator<string> Iterator = Data.iterator ();
while (Iterator.hasnext ()) {String next = Iterator.next (); IteRator.remove ();
} for (String s:data) {System.out.println (s);
}} @Test public void contains () {Data.contains ("2");
} @Test public void Deque_push () {Data.push ("dq_1");
Data.push ("dq_2");
} @Test public void Deque_pop () {//Remove and return String pop = Dequedata.pop ();
} @Test public void Deque_poll () {//Remove and return String poll = Dequedata.poll ();
} @Test public void Deque_peek () {//gets but does not remove String peek = Dequedata.peek ();
} @Test public void Deque_element () {//gets but does not remove String element = Dequedata.element ();
} @Test public void Deque_offer () {Boolean SS = Dequedata.offer ("ss"); } @Test public void Deque_remove () {