My collection Frame First play LinkedList Chapter

Source: Internet
Author: User

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&LT;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

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.