Package Com.wpr.collection;import Java.util.concurrentmodificationexception;import Java.util.iterator;import Java.util.nosuchelementexception;public class Mylinkedlist<anytype> implements iterable<anytype> { Private node<anytype> begin;private node<anytype> end;private int size;private int modcount;private static Class Node<anytype>{public AnyType data;public node<anytype> pre;public node<anytype> next;public Node (AnyType data, node<anytype> Pre, node<anytype> next) {super (); this.data = Data;this.pre = Pre;this.next = Next;}} Public Mylinkedlist () {clear ();} private void Clear () {begin = new node<anytype> (null,null, null); end = new Node<anytype> (null,begin,null); size = 0;modcount++;begin.next = end;} public int size () {return this.size;} public Boolean isEmpty () {return size==0;} Public boolean Add (AnyType x) {Add (Size (), x); return true;} Public AnyType Find (int idx) {return getnode (IDX). Data;} Public AnyType Remove (int idx) {return REmove (GetNode (IDX));} /** * Modify the value of the node according to the subscript * @param idx specific subscript * @param p New value * @return The value of the original node */public AnyType set (int idx,anytype p) {Node<a nytype> temp = getnode (idx); AnyType old = Temp.data;temp.data = P;return old;} Private AnyType Remove (node<anytype> Node) {node.pre.next = Node.next;node.next.pre = node.pre;size--;modcount++ ; return node.data;} Public iterator<anytype> Iterator () {return new Linkedlistiterator ();} Inner class private class Linkedlistiterator implements Iterator<anytype>{private node<anytype> current = begin.next;private int expectedmodcount = Modcount;private Boolean oktoremove = false; @Overridepublic Boolean hasnext () { return current! = END;} @Overridepublic AnyType Next () {if (Modcount!=expectedmodcount) {throw new Concurrentmodificationexception ();} if (!hasnext ()) {throw new Nosuchelementexception ();} AnyType item = current.data;current = Current.next;oktoremove = True;return item;} @Overridepublic void Remove () {if (Modcount!=expectedmodcount) {throwNew Concurrentmodificationexception ();} if (!oktoremove) {throw new IllegalStateException ();} MyLinkedList.this.remove (current.pre); oktoremove = False;expectedmodcount + +;}} public void Add (int idx, AnyType x) {Addbefore (GetNode (IDX), x);} /** * Join a node before the current node * @param node Current nodes * @param x node to join */private void Addbefore (node<anytype> node, AnyType x) {No de<anytype> NewNode = new node<anytype> (x, node.pre,node); newNode.pre.next = Newnode;newnode.next.pre = newnode;size++;modcount++;} Public node<anytype> getnode (int idx) {node<anytype> p;if (idx<0| | idx>size) throw new Indexoutofboundsexception (); if (IDX<SIZE/2) {p = begin.next;for (int i=0;i<idx;i++) {p = P.next;}} Else{p = end;for (int i=size;i>idx;i--) {p = P.pre;}} return p;}}
My collection Frame First play LinkedList Chapter