实现 LinkedList

版权声明: https://blog.csdn.net/u011286584/article/details/82817532

使用 LinkedList 泛型类实现 MytLinkedList,以避免与库中的相关类混淆
定期整理点滴,完善自己,今后给洋哥挣钱,陪伴着让我的小宝贝发自内心爱上笑,加油吧

这里写图片描述

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

public class MyLinkedList<AnyType> implements Iterable<AnyType> {

    private int theSize;
    private int modCount = 0;
    private Node<AnyType> beginMarker;
    private Node<AnyType> endMarker;

    private static class Node<AnyType> {
        public AnyType data;
        public Node<AnyType> prev;
        public Node<AnyType> next;
        public Node(AnyType d, Node<AnyType> p, Node<AnyType> n) {
            data = d;
            prev = p;
            next = n;
        }
    }

    public MyLinkedList() {
        doClear();
    }

    public void clear() {
        doClear();
    }

    public int size() {
        return theSize;
    }

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

    public boolean add(AnyType value) {
        add(size(), value);
        return true;
    }

    public void add(int idx, AnyType value) {
        addBefore(getNode(idx, 0, size()), value);
    }

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

    public AnyType set(int idx, AnyType value) {
        Node<AnyType> p = getNode(idx);
        AnyType old = p.data;
        p.data = value;

        return old;
    }

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

    private void addBefore(Node<AnyType> p, AnyType value) {
        Node<AnyType> newNode = new Node<>(value, p.prev, p);
        newNode.prev.next = newNode;
        p.prev = newNode;
        theSize++;
        modCount++;
    }

    private AnyType remove(Node<AnyType> p) {
        p.next.prev = p.prev;
        p.prev.next = p.next;
        theSize--;
        modCount++;

        return p.data;
    }

    private Node<AnyType> getNode(int idx) {
        return getNode(idx, 0, size() - 1);
    }

    private Node<AnyType> getNode(int idx, int lower, int upper) {
        Node<AnyType> p;
        if(idx < lower || idx > upper) {
            throw new IndexOutOfBoundsException();
        }

        if(idx < size() / 2) {
            p = beginMarker.next;
            for(int i = 0; i < idx; i++) {
                p = p.next;
            }
        } else {
            p = endMarker.next;
            for(int i = size() - 1; i > idx; i--) {
                p = p.prev;
            }
        }
        return p;
    }

    private void doClear() {
        beginMarker = new Node<AnyType>(null, null, null);
        endMarker = new Node<AnyType>(null, beginMarker, null);
        beginMarker.next = endMarker;

        theSize = 0;
        modCount++;
    }
    
    public Iterator<AnyType> iterator() {
        return new LinkedListIterator();
    }

    private class LinkedListIterator implements Iterator<AnyType> {

        private Node<AnyType> current = beginMarker.next;
        private int expectedModCount = modCount;
        private boolean okToRemmove = false;

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

        public AnyType next() {
            if(modCount != expectedModCount) {
                throw new ConcurrentModificationException();
            }

            if(!hasNext()) {
                throw new NoSuchElementException();
            }

            AnyType nextItem = current.data;
            current = current.next;
            okToRemmove = true;
            
            return nextItem;
        }

        public void remove() {
            if(modCount != expectedModCount) {
                throw new ConcurrentModificationException();
            }

            if(!okToRemmove) {
                throw new IllegalStateException();
            }

            MyLinkedList.this.remove(current.prev);
            expectedModCount++;
            okToRemmove = false;
        }

    }

}

主要设计实现 MyLinkedList 类本身,包含到端点的链,表的大小以及其他的类和方法
设计一个嵌套类 Node,包含数据以及到前一个节点和后一个节点的链,也有适当的构造方法
设计一个内部类LinkedListIterator,是一个私有类,抽象位置的概念,实现了接口Iterator,并提供next、hasNext 和 remove 方法

2018.09.22 广州

猜你喜欢

转载自blog.csdn.net/u011286584/article/details/82817532