• 双向链表的代码实现


    import java.util.ConcurrentModificationException;
    import java.util.Iterator;
    import java.util.NoSuchElementException;
    
    
    public class MyLinkedList<T> implements Iterable<T> {
        //头结点
        Node<T> beginMarker;
        //尾节点
        Node<T> endMarker;
        //链表长度
        private int theSize;
        //链表修改次数
        private int modCount;
        //定义节点类
    private  class Node<T>{
       T val;
       Node pre;
       Node next;
       public  Node(T val,Node<T> pre,Node<T> next){
           this.val=val;
           this.pre=pre;
           this.next=next;
       }
    }
    public MyLinkedList(){
        doClear();
    }
    public void Clear(){
      doClear();
    }
    private void doClear() {
        // TODO Auto-generated method stub
        beginMarker = new Node<T>(null, null, null);
        endMarker=new Node<T>(null, beginMarker, null);
        beginMarker.next=endMarker;
        theSize=0;
        modCount++;
    }
    public int size(){
        return theSize;
    }
    public boolean isEmpty(){
        return size()==0;
    }
    public boolean add(T val){
        add(size(),val);
        return true;
    }
    public  void add(int index, T val) {
        // TODO Auto-generated method stub
        addBefore(getNode(index,0,size()),val);
    }
    public T get(int index){
        return getNode(index).val;
    }
    public T set(int index,T newval){
        Node<T> node=getNode(index);
        T oldval=node.val;
        node.val=newval;
        return oldval;
    }
    public T remove(int index){
        return remove(getNode(index));
    }
    private T remove(Node<T> node) {
        node.next.pre=node.pre;
        node.pre.next=node.next;
        theSize--;
        modCount++;
        
        return node.val;
    }
    private Node<T> getNode(int index) {
        // TODO Auto-generated method stub
        
        return getNode(index,0,size()-1);
    }
    private void addBefore(Node<T> node, T val) {
        // TODO Auto-generated method stub
        Node<T> newnode=new Node<T>(val, node.pre, node);
        newnode.pre.next=newnode;
        node.pre=newnode;
        theSize++;
        modCount++;
    }
    private Node<T> getNode(int index, int begin, int end) {
        Node<T> p=null;
       if(index<begin&&index>end){
           throw new IndexOutOfBoundsException();
       }
       if(index<size()/2){
           p=beginMarker.next;
           for(int i=0;i<index;i++){
               p=p.next;
           }
    
       }else{
           p=endMarker;
           for(int j=size();j>index;j--){
               p=p.pre;
           }
       }
        return null;
    }
    public Iterator<T> iterator() {
        // TODO Auto-generated method stub
        return new LinkedListIterator();
    }
    private class LinkedListIterator implements Iterator<T>{
        private Node<T> current=beginMarker.next;
        private int expecteModeCount=modCount;
        private boolean okToRemove=false;
        public boolean hasNext() {
            // TODO Auto-generated method stub
            return beginMarker!=endMarker;
        }
    
        public T next() {
            // TODO Auto-generated method stub
            if(modCount!=expecteModeCount){
                throw new ConcurrentModificationException();
            }
            if(!hasNext()){
               throw new NoSuchElementException();
            }
            T nextItem=current.val;
            current=current.next;
            okToRemove=true;
            
            return nextItem;
        }
    
        public void remove() {
            // TODO Auto-generated method stub
            if(modCount!=expecteModeCount){
                throw new ConcurrentModificationException();
            }
            if(!hasNext()){
               throw new IllegalStateException();
            }
            MyLinkedList.this.remove(current.pre);
            expecteModeCount++;
            okToRemove=false;
        }
        
    }
    }
  • 相关阅读:
    HDU 2544 最短路
    HDU 3367 Pseudoforest
    USACO 2001 OPEN
    HDU 3371 Connect the Cities
    HDU 1301 Jungle Roads
    HDU 1879 继续畅通工程
    HDU 1233 还是畅通工程
    HDU 1162 Eddy's picture
    HDU 5745 La Vie en rose
    HDU 5744 Keep On Movin
  • 原文地址:https://www.cnblogs.com/wxw7blog/p/7597119.html
Copyright © 2020-2023  润新知