LinkedList

  • 介绍
    内部是个双向循环链表来实现的
transient int size = 0; 
transient Link voidLink;      头结点
private static final class Link {
    ET data;
    Link previous, next;
    Link(ET o, Link p, Link n) {
        data = o;
        previous = p;
        next = n;
    }
}

增: 插入结点

@Override
public void add(int location, E object) {
    if (location >= 0 && location <= size) {
        Link link = voidLink;
        if (location < (size / 2)) {
            for (int i = 0; i <= location; i++) {
                link = link.next;
            }
        } else {
            for (int i = size; i > location; i--) {
                link = link.previous;
            }
        }
        Link previous = link.previous;
        Link newLink = new Link(object, previous, link);
        previous.next = newLink;
        link.previous = newLink;
        size++;
        modCount++;
    } else {
        throw new IndexOutOfBoundsException();
    }
}

批量增加

@Override
public boolean addAll(int location, Collection collection) {
    if (location < 0 || location > size) {
        throw new IndexOutOfBoundsException();
    }
    int adding = collection.size();
    if (adding == 0) {
        return false;
    }
    Collection elements = (collection == this) ?
            new ArrayList(collection) : collection;

    Link previous = voidLink;
    if (location < (size / 2)) {
        for (int i = 0; i < location; i++) {
            previous = previous.next;
        }
    } else {
        for (int i = size; i >= location; i--) {
            previous = previous.previous;
        }
    }
    Link next = previous.next;
    for (E e : elements) {
        Link newLink = new Link(e, previous, null);
        previous.next = newLink;
        previous = newLink;
    }
    previous.next = next;
    next.previous = previous;
    size += adding;
    modCount++;
    return true;
}

删:

@Override
public E remove(int location) {
    if (location >= 0 && location < size) {
        Link link = voidLink;
        if (location < (size / 2)) {
            for (int i = 0; i <= location; i++) {
                link = link.next;
            }
        } else {
            for (int i = size; i > location; i--) {
                link = link.previous;
            }
        }
        Link previous = link.previous;
        Link next = link.next;
        previous.next = next;
        next.previous = previous;
        size--;
        modCount++;
        return link.data;
    }
    throw new IndexOutOfBoundsException();
}

查:

@Override
public E get(int location) {
    if (location >= 0 && location < size) {
        Link link = voidLink;
        if (location < (size / 2)) {
            for (int i = 0; i <= location; i++) {
                link = link.next;
            }
        } else {
            for (int i = size; i > location; i--) {
                link = link.previous;
            }
        }
        return link.data;
    }
    throw new IndexOutOfBoundsException();
}

改:

@Override
public E set(int location, E object) {
    if (location >= 0 && location < size) {
        Link link = voidLink;
        if (location < (size / 2)) {
            for (int i = 0; i <= location; i++) {
                link = link.next;
            }
        } else {
            for (int i = size; i > location; i--) {
                link = link.previous;
            }
        }
        E result = link.data;
        link.data = object;
        return result;
    }
    throw new IndexOutOfBoundsException();
}

你可能感兴趣的:(LinkedList)