Data structure (Java language)--linkedlist

Source: Internet
Author: User
Tags concurrentmodificationexception

The following is an implementation of the LinkedList generic class that can be used. Here the list class name is Mylinkedlist, avoid duplication with the class library.

Mylinkedlist is implemented as a doubly linked list, and is persisted to the reference at both ends of the table. This allows you to maintain the cost of constant time for each operation as long as the operation takes place in a known location. This known location can be an endpoint, or it can be a location specified by an iterator.

Design, mainly divided into three parts to achieve:

    1. The Mylinkedlist class itself contains the chain to both ends, the size of the table, and some methods.
    2. Node class, which may be a private nested class. A node contains data as well as chains to the previous node and to the next node, along with some appropriate construction methods.
    3. The Linkedlistiterator class, which abstracts the concept of location, is a private class and implements interface iterator. Contains the implementation of the method next (), Hasnext (), remove ().
Import Java.util.concurrentmodificationexception;import Java.util.iterator;import Java.util.nosuchelementexception;public class Mylinkedlist<anytype> implements iterable<anytype> { private int thesize;private int modcount = 0;private node<anytype> beginmarker;private node<anytype> Endmarker;private Static class Node<anytype> {public AnyType data;public node<anytype> prev;public Node< Anytype> next;public Node (AnyType D, node<anytype> P, node<anytype> n) {data = D;prev = P;next = n;}} 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 int size () {return thesize;} public Boolean isEmpty () {return size () = = 0;} public void Add (AnyType x) {Add (Size (), x);} public void Add (int idx, AnyType x) {Addbefore (GetNode (IDX), x);} Public AnyType get (int idx) {return getnode (idx). Data;} Public AnyType Set (int idx, AnyType newval) {node<anytype> p = getnode (idx); AnyType oldval = P.data;p.data = Newval;return oldval;} Public AnyType reomve (int idx) {return remove (GetNode (IDX));} private void Addbefore (Node<anytype> p, AnyType x) {node<anytype> newNode = new node<anytype> (x, P.prev , p); newNode.prev.next = Newnode;p.prev = newnode;thesize++;modcount++;} Private AnyType Remove (node<anytype> p) {p.next.prev = P.prev;p.prev.next = P.next;thesize--;modcount++;return P.data;} Private node<anytype> getnode (int idx) {node<anytype> p;if (idx < 0 | | idx > Size ()) {throw new indexout Ofboundsexception ();} if (IDX < size ()/2) {p = beginmarker.next;for (int i = 0; i < idx; i++) {p = P.next;}} else {p = endmarker;for (in t i = size (); i < idx; i--) {p = P.prev;}} return p;} Public iterator<anytype> Iterator () {return new Linkedlistiterator ();} Private class Linkedlistiterator implements iterator<anytype> {private Node<anytype> current = beginmarker.next;private int expectedmodcount = modcount;private Boolean oktoremove = False;@O Verridepublic Boolean hasnext () {return current! = Endmarker;} @Overridepublic AnyType Next () {if (Modcount! = Expectedmodcount) {throw new Concurrentmodificationexception ();} if (!hasnext ()) {throw new Nosuchelementexception ();} AnyType Nextitem = current.data;current = Current.next;oktoremove = True;return Nextitem;} public void Remove () {if (Modcount! = Expectedmodcount) {throw new Concurrentmodificationexception ();} if (!oktoremove) {throw new IllegalStateException ();} MyLinkedList.this.remove (current.prev); oktoremove = false;expectedmodcount++;}}}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Data structure (Java language)--linkedlist

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.