实现MyLinkedList

MyLinkedList:

 

package com.matt;

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

public class MyLinkedList<AnyType> implements Iterable<AnyType> {
	private int theSize;
	private int modCount = 0;
	private Node<AnyType> beginMarker;
	private Node<AnyType> endMarker;

	private static class Node<AnyType>{
		public Node(AnyType d, Node<AnyType> p, Node<AnyType> n){
			data = d; prev = p; next = n;
		}
		
		public AnyType data;
		public Node<AnyType> prev;
		public Node<AnyType> next;
	}
	
	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;
	}
	
	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;
	}
	
	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));
	}
	
	private void addBefore(Node<AnyType> p, AnyType x){
		Node<AnyType> newNode = new Node<AnyType>(x,p.prev,p);
		p.prev.next = newNode;
		p.prev = newNode;
		
		theSize++;
		modCount++;
	}
	
	private AnyType remove(Node<AnyType> p){
		p.prev.next = p.next;
		p.next.prev = p.prev;
		
		theSize--;
		modCount++;
		
		return p.data;
	}
	
	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 = size();i>idx;i--){
				p = p.prev;
			}
		}
		
		return p;
	}
	
	@Override
	public Iterator<AnyType> iterator() {
		
		return new LinkedListIterator();
	}
	
	private class LinkedListIterator implements Iterator<AnyType>{
		private Node<AnyType> current = beginMarker.next;
		private int expectedModCount = modCount;
		private boolean okToRemove = false;
		
		
		@Override
		public boolean hasNext() {
			
			return current != endMarker;
		}

		@Override
		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++;
			
		}
		
	}
	
	public String toString(){
		Node<AnyType> p = beginMarker;
		String l = "[";
		for(int i=0;i<size();i++){
			p = p.next;
			if(i<size()-1)
			l = l + p.data + ",";
			else l = l + p.data + "]";
		}
		
		return l;
	}

	public static void main(String [] args){
		MyLinkedList<String> lst = new MyLinkedList<String>();
		lst.add("a");
		lst.add("b");
		lst.add("c");
		
		System.out.println(lst.size());
		lst.remove(1);
		System.out.println(lst);
		
	    Iterator<String> lstIter = lst.iterator();
	    while(lstIter.hasNext()){
	    	String str = lstIter.next();
	    	if(str.equals("b")) lstIter.remove();
	    }
	    System.out.println(lst);
	    
	}
	
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值