The realization of linklist

Source: Internet
Author: User
Tags concurrentmodificationexception

1  Public classMylinkedlist<anytype>ImplementsIterable<anytype> {2 @Override3      PublicIterator<anytype>iterator () {4         return Newlinkedlistiterator ();5     }6 7      Public classLinkedlistiteratorImplementsIterator<anytype>{8 9         PrivateNode current =Beginmarker.next;Ten         Private intExpectedmodcount =Modcount; One         Private intOktoremove = 0; A  - @Override -          Public BooleanHasnext () { the             returnCurrent! =Endmarker; -         } -  - @Override +          PublicAnyType Next () { -             if(Modcount! =expectedmodcount) +                 Throw Newconcurrentmodificationexception (); A             if(!Hasnext ()) at                 Throw Newnosuchelementexception (); -  -AnyType element =(AnyType) current.data; -Current =Current.next; -oktoremove++; -             returnelement; in         } -  to          Public voidRemove () { +             if(Modcount! =expectedmodcount) -                 Throw Newconcurrentmodificationexception (); the             if(oktoremove>0) *                 Throw Newillegalstateexception (); $ Panax NotoginsengMylinkedlist. This. Remove (Current.prev); -oktoremove--; theexpectedmodcount++; +         } A     } the  +  -  $     Private Static classNode<anytype>{ $          PublicNode (AnyType D, node<anytype> p, node<anytype>N) { -data = D; prev = p; Next =N; -         } the          PublicAnyType data; -          PublicNode<anytype>prev;Wuyi          PublicNode<anytype>Next; the     } -  Wu     Private intthesize; -     Private intModcount = 0; About     PrivateNode<anytype>Beginmarker; $     PrivateNode<anytype>Endmarker; -  -      Public voidClear () { -Beginmarker =NewNode<> (NULL,NULL,NULL);//Endmarker is not yet defined, so it cannot be passed in as a parameter AEndmarker =NewNode<> (NULL, Beginmarker,NULL); +Beginmarker.next =Endmarker; the  -thesize = 0; $modcount++; the     } the  the      Public intsize () { the         returnthesize; -     } in  the      Public BooleanIsEmpty () { the             return(Size () = = 0); About     } the  the      PublicNode<anytype> GetNode (intidx) { the         if(IDX < 0 | | idx >=thesize) +             Throw Newindexoutofboundsexception (); -Node<anytype>Thisnode; the         if(IDX < Size ()/2){BayiThisnode =Beginmarker.next; the              for(inti = 0; i < idx; i++) theThisnode =Thisnode.next; -         } -         Else { theThisnode =Endmarker.prev; the              for(inti = size (); i > idx; i--) theThisnode =Thisnode.prev; the         } -         returnThisnode; the     } the  the      Public voidAddintidx,anytype Element) {94 Addbefore (idx,element); the     } the  the      Public voidAddbefore (intidx, AnyType Element) {98         if(IDX < 0 | | idx > Size ()) {//the most desirable value is the last one next About             Throw Newindexoutofboundsexception (); -         }101Node p =getnode (IDX);102Node current =NewNode (element,p.prev,p);103P.prev.next =Current ;104P.prev =Current ; thethesize++;106modcount++;107     }108 109      Public BooleanAdd (AnyType element) { the Add (thesize,element);111         return true; the     }113  the      PublicAnyType Set (intidx, AnyType Element) { theNode p =getnode (IDX); theAnyType Oldval =(AnyType) p.data;117P.data =element;118         returnOldval;119     } - 121      PublicAnyType Remove (intidx) {122         returnRemove (getnode (IDX));123     }124  the      PublicAnyType Remove (Node p) {126P.prev.next =P.next;127P.next.prev =P.prev; -thesize--;129modcount++; the         return(AnyType) p.data;131     } the}

The modcount is defined in the list and its iterators to mark the current operand in order to prevent the other threads from changing the list when any one of the two classes of objects is manipulating the data.
If the Modcount does not match, then the problem is leaking, which means "fast failure".
Oktoremove is designed to prevent the deletion of head nodes.

The realization of linklist

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.