LinkedList 实现

下面是LinkedList实现 

 

package c3;

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


public class MyLinkedList<AnyType > implements Iterable<AnyType> {

	private static class Node <AnyType>{
		public Node<AnyType> pre ;
		public Node<AnyType> next ;
		public AnyType data ;
		
		public Node(Node<AnyType> pre ,Node<AnyType> next, AnyType data){
			this.pre = pre;
			this.next= next;
			this.data = data ;
		}
	}
	
	private int modCount=0;
	private int theSize ;
	private Node<AnyType> beginMarker ; //头结点和尾结点不存放数据
	private Node<AnyType> endMarker;

	public MyLinkedList(){
		clear() ;
	}
	
	public void clear (){
		beginMarker = new Node<AnyType>(null, null, null) ;
		endMarker = new Node<AnyType>(beginMarker, null, null) ;
		beginMarker.next = endMarker ;  //这里要注意,不能直接在beginMarker中赋值
		theSize =0;
		modCount ++;
	}
	public int size(){
		return theSize ;
	}
	public boolean isEmpty(){
		return size()==0;
	}
	/**
	 * add to last
	 */
	public boolean add(AnyType x ){
		add(x, size()) ;
		return true ;
	}
	public void add(AnyType x ,int index){
		addBefore(getNode(index), x) ;
	}
	public AnyType get(int index ){
		return getNode(index).data;
	}
	public AnyType set(int index, AnyType newVal){
		Node<AnyType> pNode = getNode(index) ;
		AnyType oldVal = pNode.data ;
		pNode.data = newVal ;
		return oldVal;
	}
	public AnyType remove (int index ){
		return remove(getNode(index)) ;
	}
	public Node<AnyType>  getNode(int index){
		if (index<0|| index>size()){
			throw new IndexOutOfBoundsException() ;
		}
		Node<AnyType> pNode;
		
		if (index< size()/2){
			pNode= beginMarker.next;
			for (int i=0;i<index;i++){
				pNode =pNode.next;
			}
		}else {
			 pNode= endMarker; //注意上面的是beginMarker.next
			 for (int i=0;i<index ;i++){
				 pNode = pNode.pre ;
			 }
		}
		return pNode ;
	}
	/**
	 * 要改三个点
	 */
	public void addBefore(Node<AnyType> p, AnyType x ){
		Node<AnyType> newNode = new Node<AnyType>(p.pre, p, x) ; //newNode
		newNode.pre.next = newNode ;  //p前面的点
		p.pre = newNode ;  //p
		theSize++ ;
		modCount++ ;
	}
	/**
	 * 要改两个点
	 */
	private AnyType remove(Node<AnyType> p){
		p.pre.next = p.next ;
		p.next.pre = p.pre ;
		theSize --;
		modCount++;
		return p.data ;
	}
	
	public Iterator<AnyType> iterator() {
		return new MyLinkedListIterator();
	}
	
	private class MyLinkedListIterator implements Iterator<AnyType>{
		private Node<AnyType> currentNode = beginMarker.next ; 
		//第一个开始 ,类似于MyArrayList,第一次调用 next时,应该返回current中的数据
		private int expectedModCount = modCount ;
		private boolean okToRemove = false ;
		
		public boolean hasNext() {
			return currentNode!= endMarker ;
		}
		/**
		 * 在调用了next后,才可以进行remove,所以remove的是current前面的数据
		 */
		public AnyType next() {
			if (modCount!= expectedModCount){
				throw new ConcurrentModificationException() ;
			}
			if (!hasNext()){
				throw new NoSuchElementException();
			}
			AnyType nextItem = currentNode.data ;
			currentNode = currentNode.next ; //后移动一个
			okToRemove = true ;
			return nextItem;
		}

		public void remove() {
			if (modCount!= expectedModCount){
				throw new ConcurrentModificationException();
			}
			if (!okToRemove){
				throw new IllegalStateException() ;
			}
			MyLinkedList.this.remove(currentNode.pre) ;
			okToRemove = false ;
			expectedModCount ++ ;
		}
		
	}


}

  

posted @ 2015-04-26 23:25  chuiyuan  阅读(196)  评论(0编辑  收藏  举报