码迷,mamicode.com
首页 > 其他好文 > 详细

LinkedList 实现

时间:2015-04-27 00:23:07      阅读:110      评论:0      收藏:0      [点我收藏+]

标签:

下面是LinkedList实现 

 

package charpter3;

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

public class MyLinkedList<AnyType> implements Iterable<AnyType>{
	
	private static class Node <AnyType>{
		public AnyType data ;
		public Node< AnyType>  prev ;
		public Node<AnyType> next ;
		public Node(AnyType d, Node< AnyType> p, Node<AnyType> n){
			data =d ;
			prev = p;
			next = n ;
		}
	}
	
	private int theSize ;
	private int modCount =0;
	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>(null, beginMarker, null) ;
		beginMarker.next = endMarker;
		
		theSize =0;
		modCount ++;
	}
	
	public int size(){
		return theSize;
	}
	
	public boolean isEmpty (){
		return size()==0;
	}
	/**
	 * add to the last 
	 * @param x
	 * @return
	 */
	public boolean add (AnyType x ){
		add(size(), x);
		return true ;
	}
	
	public void add (int idx , AnyType x){
		addBefore(getNode(idx), x) ;
	}
	public AnyType get (int idx ){
		return getNode(idx).data ;
	}
	/**
	 * 
	 * @param idx
	 * @param newVal
	 * @return oldVal 
	 */
	public AnyType set(int idx , AnyType newVal ){
		Node<AnyType> p = getNode(idx) ;
		AnyType oldVal = p.data ;
		p.data = newVal ;
		return oldVal ;
	}
	public AnyType remove (int idx ){
		return remove(getNode(idx)) ;
	}
	
	public Node<AnyType> getNode(int idx){
		Node<AnyType> p;
		if (idx<0|| idx>size()){
			throw new IndexOutOfBoundsException() ;
		}
		//分两种,加快速度 
		if (idx>size()/2){
			p= beginMarker.next ;
			for (int i =0;i<idx;i++){
				p=p.next ;
			}
		}else {
			 p= endMarker;
			 for (int i=0;i>idx ;i--){
				 p=p.prev ;
			 }
		}
		return p;
	}
	
	
	/**
	 * before add:pre--->p
	 * after add: pre--->x--->p
	 * @param p
	 * @param x
	 */
	public void addBefore(Node<AnyType> p ,AnyType x){
		Node<AnyType> newNode = new Node<AnyType>(x, p.prev, p);
		newNode.prev.next= newNode;
		p.prev = newNode ;
		theSize++;
		modCount++;
	}
	/**
	 * pre--->p--->next
	 * @param p
	 * @return
	 */
	private AnyType remove (Node<AnyType> p){
		p.next.prev= p.prev;
		p.prev.next=p.next;
		theSize--;
		modCount++;
		return p.data ;
	}

	public Iterator<AnyType> iterator() {
		// TODO Auto-generated method stub
		return new LinkedListIterator() ;
	}
	
	private class LinkedListIterator implements Iterator<AnyType>{
		private Node<AnyType> current = beginMarker.next ;
		private int expectedModCount = modCount ;
		private boolean okToRemove = false ;
		
		public boolean hasNext() {
			// TODO Auto-generated method stub
			return current!= endMarker;
		}

		public AnyType next() {
			if (modCount!= expectedModCount){
				throw new ConcurrentModificationException() ;
			}
			if (!hasNext()){
				throw new NoSuchElementException() ;
			}
			AnyType nextItem = current.data ;
			current = current.next;
			okToRemove = true ;
			return nextItem;
		}

		@Override
		public void remove() {
			if (modCount!= expectedModCount){
				throw new ConcurrentModificationException() ;
			}
			if (!okToRemove){
				throw new IllegalStateException() ;
			}
			MyLinkedList.this.remove(current.prev) ;
			okToRemove = false ;
			expectedModCount++ ;
		}
		
	}

}

  

LinkedList 实现

标签:

原文地址:http://www.cnblogs.com/chuiyuan/p/4458714.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!