LinkList的实现

  1 public class MyLinkedList<AnyType> implements Iterable<AnyType> {
  2     @Override
  3     public Iterator<AnyType> iterator() {
  4         return new LinkedListIterator();
  5     }
  6 
  7     public class LinkedListIterator implements Iterator<AnyType>{
  8 
  9         private Node current = beginMarker.next;
 10         private int expectedModCount = modCount;
 11         private int okToRemove = 0;
 12 
 13         @Override
 14         public boolean hasNext() {
 15             return current != endMarker;
 16         }
 17 
 18         @Override
 19         public AnyType next() {
 20             if(modCount != expectedModCount)
 21                 throw new ConcurrentModificationException();
 22             if(!hasNext())
 23                 throw new NoSuchElementException();
 24 
 25             AnyType element =  (AnyType)current.data;
 26             current = current.next;
 27             okToRemove++;
 28             return element;
 29         }
 30 
 31         public void remove(){
 32             if(modCount != expectedModCount)
 33                 throw new  ConcurrentModificationException();
 34             if(okToRemove>0)
 35                 throw new IllegalStateException();
 36 
 37             MyLinkedList.this.remove(current.prev);
 38             okToRemove--;
 39             expectedModCount++;
 40         }
 41     }
 42 
 43 
 44 
 45     private static class Node<AnyType>{
 46         public Node(AnyType d, Node<AnyType> p , Node<AnyType> n){
 47             data = d; prev = p; next = n;
 48         }
 49         public AnyType data;
 50         public Node<AnyType> prev;
 51         public Node<AnyType> next;
 52     }
 53 
 54     private int theSize;
 55     private int modCount = 0;
 56     private Node<AnyType> beginMarker;
 57     private Node<AnyType> endMarker;
 58 
 59     public void clear(){
 60         beginMarker = new Node<>(null,null,null);//endMarker还没有定义,因此还不能作为参数传入
 61         endMarker = new Node<>(null,beginMarker,null);
 62         beginMarker.next = endMarker;
 63 
 64         theSize = 0;
 65         modCount++;
 66     }
 67 
 68     public int size(){
 69         return theSize;
 70     }
 71 
 72     public boolean isEmpty(){
 73             return (size() == 0);
 74     }
 75 
 76     public Node<AnyType> getNode(int idx){
 77         if(idx < 0 || idx >= theSize)
 78             throw new IndexOutOfBoundsException();
 79         Node<AnyType> thisNode;
 80         if(idx < size()/2){
 81             thisNode = beginMarker.next;
 82             for(int i = 0; i < idx; i++)
 83                 thisNode = thisNode.next;
 84         }
 85         else {
 86             thisNode = endMarker.prev;
 87             for(int i = size(); i > idx; i--)
 88                 thisNode = thisNode.prev;
 89         }
 90         return thisNode;
 91     }
 92 
 93     public void add(int idx,AnyType element){
 94         addBefore(idx,element);
 95     }
 96 
 97     public void addBefore(int idx, AnyType element){
 98         if(idx < 0 || idx > size()){ //可取的最大值是最后一个的下一个
 99             throw new IndexOutOfBoundsException();
100         }
101         Node p = getNode(idx);
102         Node current = new Node(element,p.prev,p);
103         p.prev.next = current;
104         p.prev = current;
105         theSize++;
106         modCount++;
107     }
108 
109     public boolean add(AnyType element){
110         add(theSize,element);
111         return true;
112     }
113 
114     public AnyType set(int idx, AnyType element){
115         Node p = getNode(idx);
116         AnyType oldVal = (AnyType) p.data;
117         p.data = element;
118         return oldVal;
119     }
120 
121     public AnyType remove(int idx){
122         return remove(getNode(idx));
123     }
124 
125     public AnyType remove(Node p){
126         p.prev.next = p.next;
127         p.next.prev = p.prev;
128         theSize--;
129         modCount++;
130         return (AnyType)p.data;
131     }
132 }

在链表和其迭代器中都定义modCount来标记当前操作数,是为了防止在两个类的对象中任意一个操作数据时,其他线程将链表改变,
如果modCount不匹配,则说明问题暴漏,即"快速失败".
okToRemove是为了防止删除头结点而设计.

原文地址:https://www.cnblogs.com/sunnysola/p/4821401.html