MyLinkedList

相比较myarraylist只是实现原理,myLinkedList的鲁棒性好。只是实现简单功能,部分功能没有实现。

public class MyLinkedList implements Iterable {

    /**
     * 成员变量:长度、操作次数、头节点、尾节点
     * 增删改查 都是先找到节点 在节点上操作 查询操作:时间复杂度 0(n/2);
     *
     */

    private int theSize;
    private int modCount;
    private Node beginNode;
    private Node endNode;

    public MyLinkedList(){
        doInClear();
    }

    public void clear(){
        doInClear();
    }
    public boolean isEmpty(){
        return theSize == 0;
    }
    public int size(){
        return theSize;
    }

    public Anytype get(){
        return get(theSize - 1);
    }
    public Anytype get(int index){
        return getNode(index).item;
    }
    public Anytype set(int index,Anytype item){
        Anytype p = null;
        Node node = getNode(index);
         p = node.item;
         node.item = item;
        return p;
    }
    //直接删除最后一个
    public Anytype remove(){
        Node node = endNode.pre;
        return remove(node);
    }

    private Anytype remove(Node node){
        node.next.pre = node.pre;
        node.pre.next = node.next;
        theSize --;
        modCount ++;
        return node.item;
    }
    public Anytype remove(int index){
        //getNode 会检测参数
        Node node = getNode(index);
        return remove(node);
    }
    public void add(Anytype item){
        //直接加在最后就完事了
        addBefore(endNode,item);
        modCount ++;
        theSize ++;
    }
    public void add(int index,Anytype item){
        //getNode 有index的判断。item 是否可为null 按要求而定。
//        if(item == null){
//
//        }
        addBefore(getNode(index),item);
    }
    private void addBefore(Node node, Anytype item){
        //从右向左赋值 最后修改node.pre的值。
       node.pre = node.pre.next = new Node(item,node.pre,node);
       modCount ++;
       theSize ++;
    }
    private Node getNode(int index){

        if(index <0 || index>theSize-1){
            throw new IndexOutOfBoundsException();
        }
        Node p = null ;
        if(index < theSize/2){
            //靠近beginNode
            for (int i = theSize;i>index;i--){
                p = endNode.pre;
            }
        }else{
            p = beginNode.next;
            for (int i = 0;i(null,null,null);
        endNode = new Node(null,beginNode,null);
        beginNode.next = endNode;
    }
    @NonNull
    @Override
    public Iterator iterator() {
        return null;
    }

    @Override
    public void forEach(Consumer action) {

    }

    @Override
    public Spliterator spliterator() {
        return null;
    }

    static private class Node{
        //链表节点。
        /**
         * 有三个参数 item值、previouse节点、next节点
         */
        public Anytype item;
        public Node pre;
        public Node next;
        public Node(Anytype item,Node pre,Node next){
            this.item = item;
            this.pre = pre;
            this.next = next;
        }

    }

    private class MyIterator implements Iterator{

        private int exceptedModCount;
        private Node current = beginNode.next;
        private boolean canToNext;
        public MyIterator(){
            exceptedModCount = modCount;
            canToNext  = false;
        }
        @Override
        public boolean hasNext() {
            if(exceptedModCount != modCount){
                throw new ConcurrentModificationException();
            }
            return current.next != null;
        }

        @Override
        public Anytype next() {
            if(exceptedModCount != modCount){
                throw new ConcurrentModificationException();
            }
            if(!hasNext()){
                throw new NoSuchElementException();
            }

            current = current.next;
            canToNext = true;
            return current.item;
        }

        @Override
        public void remove() {

            if(exceptedModCount != modCount){
                throw new ConcurrentModificationException();
            }
            if(!canToNext){
                throw new IllegalStateException();
            }
            MyLinkedList.this.remove(current.pre);
            canToNext = false;
            exceptedModCount ++;
        }
    }
}

你可能感兴趣的:(MyLinkedList)