Package Javabean.adt.list;import Java.util.concurrentmodificationexception;import Java.util.iterator;import Java.util.listiterator;import java.util.nosuchelementexception;/** * Analog doubly linked list * Class itself contains the chain to the ends, the size of the table, and some methods, * 2:node class, He may be a private nested class, a node that contains data as well as a chain to the previous node chain and to the next node. There are also some appropriate construction methods * 3:linkedlistiterator class, which abstracts the concept of location, is a private class, and implements interface iterator, which provides a method for next Hasenext and remove class * because the iterator class stores "current Node" Reference, and the terminal tag is a reasonable location, so it makes sense to create an extra node to represent the terminal tag at the terminal of the table, * further we can create an additional node at the top of the index, logically representing the tag that the table begins with, these additional nodes are sometimes called tag nodes, special, Nodes at the front end are sometimes called "head nodes", while nodes at the end are sometimes called tails, * The advantage of using these extra nodes is that they greatly simplify coding by sending out many special situations, such as: if we don't use the head node, then deleting the first one becomes a special case. Because in the meantime we have to readjust the chain of the linked list to the first node, * because the chain of the first node of the list is empty before the first node has no nodes, so the last node is deleted. If we're not using the tail node, then when we delete the last node, The chain of the second-to-last node will have to be set to NULL because there is no node behind it */public class Mylinkedlist<e> implements iterable<e> {private int t Hesize; private int modcount = 0; The link list head node private node<e> beginmarker; The chain footer node private node<e> endmarker; Public Mylinkedlist () {} public void Clear () {doclear (); The next node of the private void Doclear () {///head node is the tail nodes equivalent to one element in the linked list. There are only two node nods and tails Beginmarker = new node<e> (NULL, NULL, NULL); Endmarker = new Node<e> (beginmarker, NULL, NULL); Beginmarker.next = Endmarker; thesize = 0; modcount++; } public int size () {return this.thesize; } public boolean IsEmpty () {return this.size ()-0 = = 0; } public boolean Add (E e) {Add (thesize, E); return true; } public void Add (int index, E e) {checkpositionindex (index); if (thesize = = index) addlast (e); Else Addbefore (getnode (index), e); }/** * Here is to add a new node to the tail of the list * * @param e */private void AddLast (E e) {final node<e> last = Endmarker; Final node<e> NewNode = new Node<e> (last, E, null); The new node is called the head node Endmarker = NewNode; if (last = = null){//If the tail node is null to indicate that the current list is an empty list then the new node is the tail node and the head junction beginmarker = NewNode; } else {last.next = NewNode; } thesize++; modcount++; } public E Remove (int index) {checkelementindex (index); Return unlink (GetNode (index)); } Private E Unlink (node<e> node) {//In the absence of the addition of Checkelementindex this method is intended to determine whether the node is empty (if the current list has just been created and does not add any Node so the current header and tail nodes are null)//getnode I did not add any validation even if the current list is empty, the value it takes will be null//but after adding Checkelementindex to this method Index<thesi Ze//That is, the thesize is 0 o'clock will throw the wrong, is also the current list is empty does not allow deletion//So program execution here This node will not be null rest assured bold use it final E Oldanaytyp e = Node.anaytype; Final node<e> prev = Node.prev; Final node<e> next = Node.next; If a node node is a head node, the IF (prev = = null) {Beginmarker = next) can be prev if the next node of the header is placed as null } else {///If node is not a head node, then point to the next node node next to nodes, pointing to the Prev node's previous nodes//But the precondition node is not the tail node Prev.next = next; The next of node's previous nodes already has a new point so node prev is first set to null Node.prev = NULL; }//If it is a tail node then only the previous node of the tail node must be set to null the fact is that if the current node is the head node and the tail nodes, then the last node of the tail node//IS null Prev.next is not allowed to be referenced so there is a more logical If the tail node is deleted then that is the tail node of the previous node is the tail node//Because the current list of member variable tail node is allowed to be null in the same way above the head node//If not the tail nodes if (next = null) { Endmarker = prev; } else {next.prev = prev; The next node's prev also has a new pointer to node next to null node.next = NULL; }//The data field of the last node is set to null node.anaytype = NULL; thesize--; modcount++; return oldanaytype; public boolean remove (Object o) {if (o = = null) {for (node<e> node = beginmarker; Node! = N Ull node = node.next) {if (Node.anaytype = = null) {unlink (node); return true; }}} else {for (node<e> Node = Beginmarker; Node! = NULL; node = node.next) {if (O.equals (Node.anaytype)) {Unlink (node); return true; }}} return false; Public E get (int index) {checkpositionindex (index); Return GetNode (index). Anaytype; The public E set (int index, E e) {this.checkpositionindex (index); node<e> node = getnode (index); E old = Node.anaytype; Node.anaytype = e; return old; } private Boolean Ispositionindex (int index) {return index >= 0 && index <= thesize; } private void Checkpositionindex (int index) {if (!ispositionindex (index)) throw new Indexoutofbound Sexception (OUTOFBOUNDSMESG (index)); } private String OUTOFBOUNDSMESG (int index) {return index position: "+ index +", linked list length: "+ thesize; private void Checkelementindex (int index) {if (!this.iselementindex (index)) throw new Indexoutofboundsexception (OUTOFBOUNDSMESG (index)); } private Boolean Iselementindex (int index) {return index >= 0 && index < thesize; } private node<e> getnode (int index) {if (Index < (thesize >> 1)) {node<e> x = Beginmarker; for (int i = 0; i < index; i++) {x = X.next; } return x; } else {node<e> x = Endmarker; for (int i = theSize-1; i > Index; i--) {x = X.prev; } return x; }}/** * @param prev: The node on the original linked list is now going to be inserted in front of the node @param e */private void Addbefore (node<e > prev, e E) {final node<e> p = prev.prev; Final node<e> NewNode = new Node<e> (Prev.prev, E, prev); Regardless of whether Prev is the head node after the new node is inserted prev must be the next node of the new node Prev.prev = NewNode; if (p = = null) {//discovers the previous node of prev is null Representative Prev is the head node, so long as the new node becomes the head node can be beginmarker = NewNode; } else {//prev is not a head node then the prev node is replaced with a new node P.next = NewNode; } thesize++; modcount++; } @Override Public iterator<e> Iterator () {return null; } private Static Class Node<e> {//actual stored element E Anaytype; The previous node of the node node<e> prev; The next node of the node node<e> next; Node (node<e> prev, E Anaytype, node<e> next) {this.anaytype = Anaytype; This.prev = prev; This.next = Next; }} Private class Mylinkediterstor implements listiterator<e> {/** * LinkedList iterator with Arrlist's Iterators are different *; LinkedList iterators can specify an initial index * * @return */private node<e> OldNode; Private node<e> Next; private int nextidex; private int expectedmodcount = Modcount; MylinkeditErstor (int index) {nextidex = index; Next = index = = thesize? Endmarker:getnode (index); } @Override public Boolean hasnext () {/** * or front-to-back traversal */retur n Nextidex < thesize; } @Override public E next () {this.checkforcomodification (); if (!hasnext ()) throw new Nosuchelementexception (); OldNode = Next; Next = Next.next; nextidex++; return oldnode.anaytype; } private void Checkforcomodification () {if (! ( Expectedmodcount = = Modcount)) throw new Concurrentmodificationexception (); } @Override public Boolean hasprevious () {return nextidex > 0; } @Override Public E Previous () {checkforcomodification (); if (!hasprevious ()) throw new Nosuchelementexception (); /** * Analysis of node NULL may be assigned according to the current list * The first is an empty list by itself * The second is to the list head * If it is to the head of the list Then Nexindex is not a vegetarian. * Nexindex = 2 When the second node is next to last. * OldNode = penultimate node * next = head node * Then hasprevious nextindex>0;1 * Oldndoe = head node * next = NULL * next = 0; * The method is to traverse forward so the value of the OldNode is the value of the previous next * = the priority is lowest */OldNode = next = (Next = = Nu LL)? BeginMarker:next.prev; nextidex--; return oldnode.anaytype; } @Override public int nextindex () {return nextidex; } @Override public int previousindex () {//function is to return the index of the next node return nextIdex-1; }//deletion is usually done after using the iterator next or after the end of the previous///After this time, the old node should be deleted instead of the new node because after the above two methods//Next has been assigned a value New node I'm afraid it's not a good time to delete a new node.//This should be the old node, that is, the previous next should be deleted from the old node object @Override public void Remove () {//delete node in LinkedList so the Modcount action counter will certainly increase in order not to make the iterator illegal in the iterator//EXPECTEDMODC Ount must be synchronous//To determine whether the observed node is consistent with the observed nodes in LinkedList checkforcomodification (); Because I did not determine whether the incoming node is null in the Unlink method,//There is a direct call to the incoming variable in the method so the node is not allowed to have a null value//oldnode when will it be a null value? /* There is only one case where the previous operation node is used as the head node, and at the end of the OldNode it is assigned a value of NULL, but that's in the java1.8 version, and my version doesn't have a null value, previous. So think about what's going to happen to the existing nodes in the iterator after deleting the old node after the new node so-called new node because there is a different direction of traversal so so-called new nodes are not allowed to take the current iterator in the amount of Nxet value because this value we It is not known how the previous node or the latter node after the normal logical deletion of the node must be deleted node of the previous node point to the deleted node chain becomes the next node of the deleted node, and then the node is deleted from the latter node point to the chain of the deleted node Replace with the previous node of the deleted node that would have an effect on the OldNode and next in the iterator. For example, it is now using forward traversal so The current jdk1.8 is the same old node and the new node is consistent after removing the old node is actually equivalent to deleting the */if (OldNode = = null) throw New IllegalStateException (); UnlinkOldNode); node<e> lastnode = Oldnode.next; if (OldNode = = next)//If the current iterator is using a traversal that is going forward in the traversal of the currently traversed node and then is deleted the next node of the current node is to be assigned to next because next is deleted/ /next is the node to which the iterator is currently traversed and the iterator will go further to the head node direction or to the tail node direction depending on the value of next//So the current node is deleted after the next unlink and the previous node of the current node is pointed to the deleted The chain of the nodes is updated to point to the node that is deleted after the node is removed//and the next node of the node that is deleted points to the deleted node's chain update to point to the previously deleted node. So we don't have to worry about the inside of the node, we just have to update the value of the current iterator//in the next and oldnode to OK, so how do you change the values of the two references in the iterator now? Next the value of the reference to OldNode is equal then the value of next should be the next node of next because next node//In the previous pointer to the previous node of next so we need to change next to the next one of the NXT nodes, so that the next previous can continue to traverse next = Lastnode, based on the previous pointer of the next node of the//deleted node. else//If the deleted node is now being traversed backwards, will it affect the value of the current next node and the Oldndoe node?//First or analysis unlink will help me complete the LinkedList node But the next node in the iterator//next needs to be the next node of the current node and each time after next, the value of next is not the value of the current traversal node, but the value of the next node of the current node/ And OldNode is the current node so delete whenThe former node is actually deleted OldNode does not have any effect on the next traversal//Because the next traversal condition is that the thesize must be reduced by 1 after nextindex< thesize is deleted in order to synchronize Thesize Nextin Dex must also be--; Questions? So in the forward traversal when the node is deleted so that the number of nodes in the LinkedList thesize will be reduced so that in order to synchronize need not sync/why sync? The data is deleted when you walk forward, why don't you need next--? Because in fact, in the convenience of moving forward we can imagine//this is to gradually discard the back node although the thesize of the node list is deleted, if this time in the go forward traversal This causes threading problems//simultaneous operation of the same nextindex situation itself this linked ' list does not have the functionality of thread-safe, nextidex--; OldNode = null; expectedmodcount++; }/** * see if the additions and deletions between iterators and the original set are synchronized */public void Checkmodicationlistator () {if (expe Ctedmodcount! = modcount) throw new Concurrentmodificationexception ("the iteration does not allow the deletion of the original collection"); }/** * This method is to re-assign a value at the time of the traversal of the assigned node is oldNode but OldNode can be deleted after being deleted is * is assigned to NULL * * @param e */@Override public void set (E e) {if (OldNode = = null) throw new IllegalStateException (); Checkforcomodification (); Oldnode.anaytype = e; }//What is the meaning of this method because the iterator does not allow linkelist to manipulate the data, so add this Add method here//actually insert into the back of the LinkedList @Override public void Add (e e) {//To add a node the old node certainly doesn't make any sense because the old node is also based on the next standard now//the position of inserting the new node is before the next node that old section The point is probably no longer the old node//Checkmodicationlistator (); OldNode = null; /** * Why do you want to know if Next is null because next is not empty then we are going to plug the new node before the next node * then to insert in the next method is actually to update the prev in the next node The IOUs property then references a null instance so it's definitely not going to work. * So be sure to determine if Next is null * Then when next can be null, there are only three places in the iterator that can be assigned to next. , Next method * Previous time, * then in the above three places when it is possible to assign a null? * The first constructor is initialized as long as the list is not empty, then the construction is used When initializing an iterator, it verifies that the linked list is not without data * If the validation failure list is an empty list then it cannot be initialized, so in the first place the constructor is not assigned null * then Next method Each time the next node of next is assigned next, then the last end is traversed to the tail node.Time * Because the next pointer property of the tail node is NULL then it is well-deserved that next will be assigned a value of NULL * So the first case is that next will appear in the tail node of the list, NE The XT is assigned a value of NULL * * * Then previous will not appear next null is worth the case the initial situation will not appear then it's time to look through the head node. It does appear that next is null but I have controlled in the program that next is null when Next is assigned to the head node * So next is null when there is only one case that is the current next has traversed the tail node part * So if Next is null, then it is traversing to the tail node, then the AddLast method in the linked list is used when inserting the node. * If Next is not NULL, the node can be inserted in front of next, then call ADDBEF Ore method * */if (next = null) AddLast (e); else Addbefore (next,e); nextidex++; modcount++; } }}
Java implementation of Linkedlilst