zoukankan      html  css  js  c++  java
  • 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 ++ ;
    		}
    		
    	}
    
    
    }
    

      

  • 相关阅读:
    jvm调试相关:jmap失效下找到alternatives神器
    作业2:java内存模型图示
    Python脚本:Linux自动化执行Python脚本
    Nodejs:单线程为什么能支持高并发?
    作业1:java虚拟机内存模型图示
    SpringBoot中获取spring.profiles.active的值
    线程中使用注解出现空指针如何解决?
    java7:核心技术与最佳实践读书笔记——对象生命周期
    java7:核心技术与最佳实践读书笔记——类加载
    虚拟机上不了网啦
  • 原文地址:https://www.cnblogs.com/chuiyuan/p/4458714.html
Copyright © 2011-2022 走看看