zoukankan      html  css  js  c++  java
  • 双向链表的代码实现

    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;
        }
        
    }
    }
  • 相关阅读:
    @字节跳动8年老Android面试官谈;Context都没弄明白凭什么拿高薪?
    @阿里面试官:Android面试这些原理都给我讲明白了,最低都是20k起步!
    @以后面试官再问你三次握手和四次挥手,直接把这一篇文章丢给他
    @备战2020年金三银四,看这一篇面试文章就够了(合适各级Java人员)
    字节跳动面试,第三面挂了,这原因我服了!
    太可惜了,四面字节跳动,我的offer竟被一道“算法题”给拦截了
    @java2019面试题北京
    @2019.07 Android 面试真题集锦
    2018 Java线程热门面试题,你知道多少?
    阿里大厂的148道核心面试题,(程序员必备学习方向)offer收割机 全会月薪50k不难
  • 原文地址:https://www.cnblogs.com/wxw7blog/p/7597119.html
Copyright © 2011-2022 走看看