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

My集合框架第一弹 LinkedList篇

时间:2015-03-02 20:32:40      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:

package com.wpr.collection;

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

public class MyLinkedList<AnyType> implements Iterable<AnyType> {
	private Node<AnyType> begin;
	private Node<AnyType> end;
	private int size;
	private int modCount;
	
	private static class Node<AnyType>{
		public AnyType data;
		public Node<AnyType> pre;
		public Node<AnyType> next;
		public Node(AnyType data, Node<AnyType> pre, Node<AnyType> next) {
			super();
			this.data = data;
			this.pre = pre;
			this.next = next;
		}
	}
	
	public MyLinkedList(){
		clear();
	}

	private void clear() {
		begin = new Node<AnyType>(null,null, null);
		end = new Node<AnyType>(null,begin,null);
		size = 0;
		modCount++;
		begin.next = end;
	}
	
	public int size(){
		return this.size;
	}
	public boolean isEmpty() {
		return size==0;
	}
	
	public boolean add(AnyType x){
		add(size(),x);
		return true;
	}

	public AnyType find(int idx){
		return getNode(idx).data;
	}
	
	public AnyType remove(int idx){
		return remove(getNode(idx));
	}
	/**
	 * 根据下标修改节点的值
	 * @param idx  具体下标
	 * @param p	   新的值
	 * @return  原来节点的值
 	 */ 
	public AnyType set(int idx,AnyType p){
		Node<AnyType> temp = getNode(idx);
		AnyType old = temp.data;
		temp.data = p;
		return old;
	}
	private AnyType remove(Node<AnyType> node) {
		node.pre.next = node.next;
		node.next.pre = node.pre;
		size--;
		modCount++;
		return node.data;
	}
	public Iterator<AnyType> iterator(){
		return new LinkedListIterator();
	}
	//内部类
	private class LinkedListIterator implements Iterator<AnyType>{
		private Node<AnyType> current = begin.next;
		private int expectedModCount = modCount;
		private boolean okToRemove = false;
		
		@Override
		public boolean hasNext() {
			return current != end;
		}

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

		@Override
		public void remove() {
			if(modCount!=expectedModCount){
				throw new ConcurrentModificationException();
			}
			if(!okToRemove){
				throw new IllegalStateException();
			}
			
			MyLinkedList.this.remove(current.pre);
			okToRemove = false;
			expectedModCount ++;
		}
		
	}
	public void add(int idx, AnyType x) {
		addBefore(getNode(idx),x);
	}
	/**
	 * 在当前节点之前加入一个一节点
	 * @param node 当前节点
	 * @param x	要加入的节点
	 */
	private void addBefore(Node<AnyType> node, AnyType x) {
		Node<AnyType> newNode = new Node<AnyType>(x, node.pre,node);
		newNode.pre.next = newNode;
		newNode.next.pre = newNode;
		size++;
		modCount++;
	}

	public Node<AnyType> getNode(int idx) {
		Node<AnyType> p;
		if(idx<0||idx>size)
			throw new IndexOutOfBoundsException();
		
		if(idx<size/2){
			p = begin.next;
			for(int i=0;i<idx;i++){
				p = p.next;
			}
		}else{
			p = end;
			for(int i=size;i>idx;i--){
				p = p.pre;
			}
		}
		return p;
	}
}

 

My集合框架第一弹 LinkedList篇

标签:

原文地址:http://www.cnblogs.com/kakaxisir/p/4309544.html

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