zoukankan      html  css  js  c++  java
  • My集合框架第一弹 LinkedList篇

    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;
    	}
    }




    在以上的链表结构基础上,讨论链表的其他操作 1.链表的反转   提供一种方法:就地反转   begin-->1-->2-->3-->end   begin-->2-->1-->3-->end   begin-->3-->2-->1-->end   代码如下:  
    	/**
    	 * 反转链表
    	 * @return
    	 */
    	public MyLinkedList reverse(){
    		Node<AnyType> cur = begin.next;
    		if(cur==end)
    			return this;
    		Node<AnyType> curNext;
    		
    		while(cur.next!=end){
    			curNext = cur.next;
    			curNext.next.pre = cur;
    			cur.next =curNext.next;
    			curNext.next = begin.next;
    			curNext.pre = begin;
    			begin.next = curNext;
    			curNext.next.pre = curNext;
    		}
    		return this;
    	}
    	
    	public static void main(String[] args) {
    		MyLinkedList<Integer> l = new MyLinkedList<>();
    		for(int i=0;i<10;i++){
    			l.add(i);
    		}
    		l = l.reverse();
    		for(Integer i:l){
    			System.out.print(i+"	");
    		}
    	}
    




  • 相关阅读:
    Access restriction: The type BASE64Encoder is not accessible due to restrict(转载)
    ECLIPSE控制台信息导出
    ZUI分页器的使用案例(ECLIPSE SMS项目)
    关于PLSQL启动用时较长的问题解决
    javax.naming.NamingException: Cannot create resource instance报错修改
    百度AI人脸识别的学习总结
    ECharts3.0饼状图使用问题总结
    jni开发
    AndroidStudio封装jar并混淆
    Androidstudio代码混淆
  • 原文地址:https://www.cnblogs.com/kakaxisir/p/4309544.html
Copyright © 2011-2022 走看看