双向链表的代码实现

import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.NoSuchElementException;


public class MyLinkedList<T> implements Iterable<T> {
    //头结点
    Node<T> beginMarker;
    //尾节点
    Node<T> endMarker;
    //链表长度
    private int theSize;
    //链表修改次数
    private int modCount;
    //定义节点类
private  class Node<T>{
   T val;
   Node pre;
   Node next;
   public  Node(T val,Node<T> pre,Node<T> next){
       this.val=val;
       this.pre=pre;
       this.next=next;
   }
}
public MyLinkedList(){
    doClear();
}
public void Clear(){
  doClear();
}
private void doClear() {
    // TODO Auto-generated method stub
    beginMarker = new Node<T>(null, null, null);
    endMarker=new Node<T>(null, beginMarker, null);
    beginMarker.next=endMarker;
    theSize=0;
    modCount++;
}
public int size(){
    return theSize;
}
public boolean isEmpty(){
    return size()==0;
}
public boolean add(T val){
    add(size(),val);
    return true;
}
public  void add(int index, T val) {
    // TODO Auto-generated method stub
    addBefore(getNode(index,0,size()),val);
}
public T get(int index){
    return getNode(index).val;
}
public T set(int index,T newval){
    Node<T> node=getNode(index);
    T oldval=node.val;
    node.val=newval;
    return oldval;
}
public T remove(int index){
    return remove(getNode(index));
}
private T remove(Node<T> node) {
    node.next.pre=node.pre;
    node.pre.next=node.next;
    theSize--;
    modCount++;
    
    return node.val;
}
private Node<T> getNode(int index) {
    // TODO Auto-generated method stub
    
    return getNode(index,0,size()-1);
}
private void addBefore(Node<T> node, T val) {
    // TODO Auto-generated method stub
    Node<T> newnode=new Node<T>(val, node.pre, node);
    newnode.pre.next=newnode;
    node.pre=newnode;
    theSize++;
    modCount++;
}
private Node<T> getNode(int index, int begin, int end) {
    Node<T> p=null;
   if(index<begin&&index>end){
       throw new IndexOutOfBoundsException();
   }
   if(index<size()/2){
       p=beginMarker.next;
       for(int i=0;i<index;i++){
           p=p.next;
       }

   }else{
       p=endMarker;
       for(int j=size();j>index;j--){
           p=p.pre;
       }
   }
    return null;
}
public Iterator<T> iterator() {
    // TODO Auto-generated method stub
    return new LinkedListIterator();
}
private class LinkedListIterator implements Iterator<T>{
    private Node<T> current=beginMarker.next;
    private int expecteModeCount=modCount;
    private boolean okToRemove=false;
    public boolean hasNext() {
        // TODO Auto-generated method stub
        return beginMarker!=endMarker;
    }

    public T next() {
        // TODO Auto-generated method stub
        if(modCount!=expecteModeCount){
            throw new ConcurrentModificationException();
        }
        if(!hasNext()){
           throw new NoSuchElementException();
        }
        T nextItem=current.val;
        current=current.next;
        okToRemove=true;
        
        return nextItem;
    }

    public void remove() {
        // TODO Auto-generated method stub
        if(modCount!=expecteModeCount){
            throw new ConcurrentModificationException();
        }
        if(!hasNext()){
           throw new IllegalStateException();
        }
        MyLinkedList.this.remove(current.pre);
        expecteModeCount++;
        okToRemove=false;
    }
    
}
}

 

posted @ 2017-09-26 15:39  竹马今安在  阅读(429)  评论(0编辑  收藏  举报