Basic implementation of Java class library linklist

Source: Internet
Author: User
Tags prev concurrentmodificationexception

After writing debugging for a long time, the border is not good processing, detailed see JDK class library, the following is only the basic implementation:
Import java.util.iterator;/** * Class Name: Mylinkedlist Description: LinkedList Basic Implementation */public class mylinkedlist<anytype> Implements iterable {private int thesize = 0;private int modcount = 0;private node<anytype> beginmarker;private Node <AnyType> endmarker;private Static class Node<anytype> {public Node (AnyType data, node<anytype> prev, Node<anytype> next) {this.data = Data;this.prev = Prev;this.next = Next;} Public AnyType data;public node<anytype> prev;public node<anytype> Next;} Public Mylinkedlist () {clear ();} public void Clear () {Beginmarker = new node<anytype> (null, NULL, NULL); endmarker = new node<anytype> (null, Beg Inmarker, null); Beginmarker.next = Endmarker;thesize = 0;modcount++;} public Boolean isEmpty () {return thesize = = 0;} public int size () {return thesize;} Public boolean Add (AnyType x) {Add (thesize, x); return true;} public void Add (int index, AnyType x) {Addbefore (GetNode (index), x);} Public AnyType get (int index) {return GetNode (Index). Data;} Private node<anytype> getnode (int index) {if (Index < 0 | | index > thesize) throw new indexoutofboundsexception (); Node<anytype> p;if (Index < size ()/2) {p = beginmarker.next;for (int i = 0; i < index; i++) {p = P.next;}} E LSE {p = endmarker;for (int i = thesize; i > Index; i--) p = P.prev;} return p;} private void Addbefore (Node<anytype> p, AnyType x) {node<anytype> newNode = new Node (x, P.prev, p); newnode.pre V.Next = Newnode;p.prev = newnode;thesize++;modcount++;} Private AnyType Remove (node<anytype> p) {AnyType removed = P.data;p.prev.next = P.next;p.next.prev = P.prev;thesize --;modcount++;return removed;} Public AnyType Set (int index, AnyType x) {AnyType old = getnode (index). Data;getnode (index). data = X;return old;} Public AnyType Remove (int index) {return remove (GetNode (index));} @Overridepublic Iterator Iterator () {//TODO auto-generated method Stubreturn new Iterator () {//Anonymous inner class implementation, The JDK is a private class that returns a re-write. Private Node<anytype> curRent = beginmarker.next;private int expectedmodcount = modcount;private Boolean oktoremove = False;public Boolean HasNext ( ) {return current! = Endmarker;} Public AnyType Next () {if (Modcount! = Expectedmodcount) {throw new Java.util.ConcurrentModificationException ();} if (!hasnext ()) throw new Java.util.NoSuchElementException (); AnyType d = current.data;current = Current.next;oktoremove = True;return D;} public void Remove () {if (Modcount! = Expectedmodcount) {throw new Java.util.ConcurrentModificationException ();} if (!oktoremove) throw new IllegalStateException (); MyLinkedList.this.remove (current.prev); oktoremove = false;expectedmodcount++;}};} public string toString () {string s = new String (), for (int i = 0; i < thesize; i++) s + = Get (i) + ""; return s;} /** * Method Name: Mylinkedlist.java Description: Test */public static void Main (string[] args) {//TODO auto-generated method Stubmylinkedlist <Integer> list = new Mylinkedlist (); List.add (1); List.add (2); List.add (3); List.add (4); List.add (5); System.out.println (list); List.remove (1); SYSTEM.OUT.PRINTLN (list); List.set (1, 2); SYSTEM.OUT.PRINTLN (list); Iterator ite = List.iterator (); while (Ite.hasnext ()) {System.out.println (Ite.next () + "");}}


Basic implementation of Java class library linklist

Related Article

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.