LinkedList的理解和代碼自己實現

在實現LinkedList的時候主要考慮三個類:

1.MyLinkedList類本身,它包含了兩端的鏈、標的大小以及一些方法,在鏈的兩端加了兩個額外的節點,頭結點(header node)和尾節點(tail node)用來標示鏈頭和鏈尾。

2.Node類,它可能是一個私有的嵌套類。一個節點包含數據以及前一個節點的連接和道下一個節點的連接,還有一個構造方法。

3.LinkedListIterator類,該類抽象了位置的概念,是一個私有類,並實現接口Iterator。提供了hasNext()、next()和remove()方法。

具體的看下面的代碼:

 

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

/**
 * @author QuLei
 *
 * @param <AnyType>泛型表示任意的類型均可以放入容器內
 * 自己實現LinkedList
 */
public class MyLinkedList<AnyType> implements Iterable<AnyType> {
	private int theSize;
	private int modCount;
	//創建兩個額外的節點一個作爲頭結點,一個作爲尾節點
	private Node<AnyType> beginMarker;
	private Node<AnyType> endMarker;
	
	/**
	 * @author QuLei
	 *
	 * @param <AnyType>
	 * 定義一個節點,表示鏈表的存儲結構
	 */
	private static class Node<AnyType>{
		public AnyType data;
		public Node<AnyType> prev;
		public Node<AnyType> next;
		public Node(AnyType data, Node<AnyType> prev, Node<AnyType> next) {
			super();
			this.data = data;
			this.prev = prev;
			this.next = next;
		}
	}
	
	
	

	public MyLinkedList() {
		clear();
	}
	
	/**
	 * change the size of the collection to zero
	 */
	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;
	}
	/**調用此方法就是直接在尾部添加
	 * @param x 要添加的元素
	 * @return 成功返回true,若失敗會拋異常出去
	 */
	public boolean add(AnyType x) {
		add(size(),x);
		return true;
	}
	/**
	 * @param idx
	 * @param x
	 */
	public void add(int idx,AnyType x) {
		addBefore(getNode(idx),x);
	}
	
	/**修改指定位置的值
	 * @param idx
	 * @param newVal
	 * @return
	 */
	public AnyType set(int idx,AnyType newVal) {
		Node<AnyType> p = getNode(idx);
		AnyType oldVal = p.data;
		p.data = newVal;
		return oldVal;
	}
	
	/**刪除指定位置的元素
	 * @param idx
	 * @return
	 */
	public AnyType remove(int idx) {
		return remove(getNode(idx));
	}
	/**查詢指定位置的值
	 * @param idx
	 * @return
	 */
	public AnyType get(int idx) {
		return getNode(idx).data;
	}
	
	/**判斷是否包含某個元素
	 * @param x  要判斷的元素
	 * @return  返回是否包含該元素
	 */
	public boolean contains(AnyType x) {
		Node<AnyType> p = beginMarker.next;
		while(p != endMarker && !(p.data.equals(x))) {
			p = p.next;
		}
		return (p != endMarker);
	}
	/**在給定節點前面插入元素
	 * @param p
	 * @param x
	 */
	private void addBefore(Node<AnyType> p,AnyType x) {
		Node<AnyType> newNode = new Node<AnyType>(x, p.prev, p);
		//newNode.prev = p.prev;   這兩句代碼可以直接放入構造器中
		//newNode.next = p;
		p.prev = p.prev.next = newNode; //把原來鏈中的結構鏈接上
		theSize ++;
		modCount ++;
	}
	private AnyType remove(Node<AnyType> p) {
		p.prev.next = p.next;
		p.next.prev = p.prev;
		theSize --;
		modCount ++;
		return p.data;
	}
	/**獲取指定位置的節點
	 * @param idx  :指定的位置
	 * @return  :返回該指定位置處的節點
	 */
	private Node<AnyType> getNode(int idx){
		Node<AnyType> p;
		if(idx < 0 || idx > size()) {
			throw new IndexOutOfBoundsException();
		}
		if(idx < size()/2) {
			p = beginMarker;
			for(int i = 0 ; i < idx;i++ ) {
				p = p.next;
			}
		}else {
			p = endMarker;
			for(int i = size();i > idx;i--) {
				p = p.prev;
			}
		}
		return p;
	}
	@Override
	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 exceptedModCount = modCount;
		private boolean okToRemove = false;
		@Override
		public boolean hasNext() {
			// TODO Auto-generated method stub
			return current != endMarker; 
		}

		@Override
		public AnyType next() {
			// TODO Auto-generated method stub
			if(modCount != exceptedModCount) {
				throw new ConcurrentModificationException();
			}
			if(!hasNext()) {
				throw new NoSuchElementException();
			}
			AnyType nextItem = current.data;
			current = current.next;
			okToRemove = true;
			return nextItem;
		}
		
		public void remove() {
			if(modCount != exceptedModCount) {
				throw new ConcurrentModificationException();
			}
			if(!okToRemove) {
				throw new IllegalStateException();
			}
			MyLinkedList.this.remove(current.prev);
			okToRemove = false;
			exceptedModCount ++;
		}
	}

}


  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章