看着书自己写了一遍linkedlist,以及其中的迭代器的实现
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class MyLinkedList<T> implements Iterable<T> {
//声明必要的变量
private int modCount = 0;//链表自从被构建以来,对链表所做的改变的次数,用于检测迭代器中的删除操作是否可以执行
private int theSize;//链表的大小
private Node<T> beginMarker;//链表的头指针
private Node<T> endMarker;//链表的尾指针
private static class Node<T>
{
public Node(T d, Node<T> p,Node<T> n)
{data = d;prev = p;next = n;}
public T data;
public Node<T> prev;//由于java Collection中的LinkedList采用的是双链表,因此每个节点需要有前驱和后继指针
public Node<T> next;
}//对于每一个节点采用静态内部类
public MyLinkedList(){ doClear();}
public void clear(){ doClear();}
public int size(){return theSize;}
@Override
public String toString() {//重写toStirng方法用来遍历链表
// TODO Auto-generated method stub
Node<T> p = beginMarker.next;
String str = "";
for(int i = 0 ; i < size() ; i++)
{
str+=p.data.toString();
str+=";";
p = p.next;}
return str;
}
public boolean isEmpty(){return size()==0;}
public boolean add(T x){add(size(),x);return true;}//在链表的尾部插入节点
public void add(int idx, T x){addBefore(getNode(idx,0,size()),x);}//在指定的索引处添加节点
public T get(int idx){ return getNode(idx).data;}
public T set(int idx, T newValue){T oldValue = getNode(idx).data; getNode(idx).data = newValue; return oldValue;}
public T remove(int idx){return remove(getNode(idx));}
private T remove(Node<T> p){
p.prev.next = p.next;
p.next.prev = p.prev;
theSize--;
modCount++;
return p.data;
}
private void addBefore(Node<T> p , T x){
Node<T> newNode = new Node(x,p.prev,p);
newNode.prev.next = newNode;
newNode.next.prev = newNode;
theSize++;
modCount++;
}
private Node<T> getNode(int idx){ return getNode(idx,0,size()-1);}
private Node<T> getNode(int idx,int lower, int upper){
Node<T> p ;
if(idx > upper ||idx < lower)
throw new IndexOutOfBoundsException();
//如果idx的值在前半部分,就从前向后遍历,如果在后半部分就从后向前遍历
if(idx < size()/2)
{
p = beginMarker.next;
for(int i = 0 ; i < idx ; i++)
p = p.next;
}
else{
p = endMarker; //p = endMarker.prev;
for(int i = size(); i > idx ; i--) //for(int i = size()-1 ; i >idx ; i--)
p= p.prev; // p = p.prev;
}
return p;
}
private void doClear()
{
beginMarker = new Node<T>(null,null,null);
endMarker = new Node<T>(null,beginMarker,null);
beginMarker.next = endMarker;
theSize = 0;
modCount++;
}
@Override
//通过调用iterator函数生成当前这个链表的迭代器
public Iterator<T> iterator() { return new LinkedListIterator();}
//用内部类继承接口实现迭代器
private class LinkedListIterator implements Iterator{
private Node<T> current = beginMarker.next;//用来表示迭代器当前的位置
private int expectedModCount = modCount;//用来检测在迭代器进行remove,next之前有没有执行链表的其他的操作
private boolean okToRemove = false;
@Override
public boolean hasNext() {
// TODO Auto-generated method stub
return current!=endMarker;
}
@Override
public Object next() {
// TODO Auto-generated method stub
if(modCount != expectedModCount)
throw new ConcurrentModificationException();//说明在迭代器进行遍历之前对链表进行了操作,因此不能在迭代器中进行查找下一项和删除的操作
if(!hasNext())
throw new NoSuchElementException();
T nextItem = current.data;
current = current.next;
okToRemove = true;//表示当前节点的状态是可以删除的
return nextItem;
}
@Override
public void remove() {
// TODO Auto-generated method stub
if(modCount != expectedModCount)
throw new ConcurrentModificationException();
if(!okToRemove)
throw new IllegalStateException();
MyLinkedList.this.remove(current.prev);
expectedModCount++;//同步expectedModCount和modCount
okToRemove = false;//当前的项已经删除因此不可再进行删除,把okToRemove改为false
}
}
public static void main(String[] args) {
MyLinkedList<Integer> list = new MyLinkedList<Integer>();
list.add(10);
list.add(20);
list.add(30);
System.out.println(list);//输出结果10;20;30
}
}