简单实现MyLinkedList

package cn.limbo.java_structure;

import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.NoSuchElementException;

/**
 * 1:首先包含到两端的链,表的大小以及其他方法
 * 2:Node类,可以设计为一个私有的嵌套类(内部类).一个节点包含数据以及
 * 到前一个节点的链和到下一个节点的链,必须要有构造方法
 * 3:LinkedListIterator类,该类抽象了位置的概念,是一个私有类,并且实现了
 * 接口Iterator,并且提供了next hasNext remove方法.
 * 主要解决前后是否有节点的问题
 * 1:区别节点和当前元素
 * 2:链表结构基本操作都是对引用的操作
 */
public class MyLinkedList implements Iterable {

    private int theSize;//表示当前数组的长度

    private int modCount = 0;

    private Node beginMarker;

    private Node endMarker;

    public MyLinkedList() {
        clear();
    }

    //不同于MyArrayList

    /**
     * change the size if this collection to zero
     */
    private void clear() {
        beginMarker = new Node<>(null, null, null);
        endMarker = new Node<>(null, beginMarker, null);
        beginMarker.next = endMarker;

        theSize = 0;
        modCount++;
    }

    public int size() {
        return theSize;
    }

    public boolean isEmpty() {
        return size() == 0;
    }

    public boolean add(T t) {
        add(size(), t);
        return true;
    }

    private void add(int idx, T t) {
        addBefore(getNode(idx), t);
    }

    public T get(int idx) {
        return getNode(idx).data;
    }

    public T set(int idx, T newVal) {
        Node p = getNode(idx);
        T oldVal = p.data;
        p.data = newVal;
        return oldVal;
    }

    public T remove(int idx) {
        return remove(getNode(idx));
    }

    private void addBefore(Node p, T x) {
        //向一个双链表中的插入操作到p点的位置
        Node newNode = new Node(x, p.prev, p);
        newNode.prev.next = newNode;
        p.prev = newNode;
        theSize++;
        modCount++;
    }

    /**
     * 从一个双链表中删除由p指定的节点
     */
    private T remove(Node p) {
        p.next.prev = p.prev;
        p.prev.next = p.next;
        theSize--;
        modCount--;
        return p.data;
    }

    /**
     * Get the Node at position idx,which must range from 0 to size().
     */
    private Node getNode(int idx) {
        Node p;
        if (idx < 0 || idx > size()) {
            throw new IndexOutOfBoundsException();
        }
        if (idx < size() / 2) {
            //从头开始节点开始想回遍历
            p = beginMarker.next;
            for (int i = 0; i < idx; i++) {
                p = p.next;
            }
        } else {
            p = endMarker;
            for (int i = size(); i > idx; i--) {
                p = p.prev;
            }
        }
        return p;
    }

    @Override
    public Iterator iterator() {
        return new LinkedListIterator();
    }

    //私有节点类
    private static class Node {

        public T data;
        public Node prev;
        public Node next;

        public Node(T t, Node p, Node n) {
            data = t;
            prev = p;
            next = n;
        }
    }

    private class LinkedListIterator implements Iterator {

        private Node current = beginMarker.next;

        private int expectedModCount = modCount;

        private boolean okToRemove = false;

        public boolean hasNext() {
            return current != endMarker;
        }

        public T next() {
            if (modCount != expectedModCount) {
                throw new ConcurrentModificationException();
            }
            if (!hasNext()) {
                throw new NoSuchElementException();
            }
            T nextItem = current.data;
            current = current.next;
            okToRemove = true;
            return nextItem;
        }

        public void remove() {
            if (modCount != expectedModCount) {
                throw new ConcurrentModificationException();
            }
            if (!okToRemove ) {
                throw new IllegalStateException();
            }
            MyLinkedList.this.remove(current.prev);
            okToRemove=false;
            expectedModCount++;
        }
    }


}

你可能感兴趣的:(java并发)