LinkedList实现

来源:互联网 发布:olay大红瓶有用吗 知乎 编辑:程序博客网 时间:2024/05/21 18:40
import java.util.ConcurrentModificationException;import java.util.Iterator;import java.util.NoSuchElementException;public class MyLinkedList<E> implements Iterable<E>{    private static class Node<E>{        public E  data;        public Node<E> prev;        public Node<E> next;        public Node(E e, Node<E> p,Node<E> n){            this.data=e;            this.prev=p;            this.next=n;        }    }    private int size;    private int count=0;    private Node<E> begin;    private Node<E> end;    public MyLinkedList() {        doClear();    }    public void clear() {        doClear();    }    private void doClear() {        this.begin=new Node<E>(null,null,null);        this.end=new Node<E>(null,begin,null);        begin.next=end;        this.size=0;        count++;    }    public int size() {        return this.size;       }    public boolean isEmpty() {        return size()==0;    }    private void addBefore(Node<E> p,E e) {        Node<E> newNode=new Node<E>(e,p.prev,p);        newNode.prev.next=newNode;        p.prev=newNode;        size++;        count++;    }    public boolean add(E e) {        add(size(),e);        return true;    }    public void add(int index,E e) {        addBefore(getNode(index,0,size()),e);    }    public  E  get(int index) {        return getNode(index).data;    }    public E set(int index,E newE) {        Node<E> oldNode=getNode(index);        E  oldE=oldNode.data;        oldNode.data=newE;        return oldE;    }    public E remove(int  index) {        return  remove(getNode(index));    }    private E remove(Node<E> p) {        p.next.prev=p.prev;        p.prev.next=p.next;        size--;        count++;        return p.data;    }    private Node<E>  getNode(int index,int low,int high){        Node<E>  p;        if(index<low||index>high) {            throw new IndexOutOfBoundsException();        }        if(index<size()/2) {            p=begin.next;            for(int i=0;i<index;i++) {                p=p.next;            }        }else {            p=end;            for(int i=size();i>index;i--) {                p=p.prev;            }        }        return p;    }    private Node<E> getNode(int index){        return  getNode(index,0,size()-1);    }    @Override    public Iterator<E> iterator() {        return  new LinkedListIterator();    }    private class LinkedListIterator  implements Iterator<E>{        private Node<E>  current =begin.next;        private int exceptedCount=count;        private boolean okToRemove=false;        @Override        public boolean hasNext() {            return current!=end;        }        @Override        public E next() {            if(count!=exceptedCount) {                throw new ConcurrentModificationException();            }            if(!hasNext()) {                throw new NoSuchElementException();            }            E next=current.data;            current=current.next;            okToRemove=true;            return next;        }        public void remove() {            if(count!=exceptedCount) {                throw new ConcurrentModificationException();            }            if(!okToRemove) {                throw new IllegalStateException();            }            MyLinkedList.this.remove(current.prev);            exceptedCount++;            okToRemove=false;        }    }}
原创粉丝点击