简单的实现linkedlist:
1、提供ListIterator的实现(包括hasNext、next、hasPrevious、previous、nextIndex、previousIndex、add、set、remove)。
2、提供size方法。
package Collection;
import java.util.AbstractSequentialList;
import java.util.ListIterator;
/**
*
* @author lichunguang
* @param <E>
*/
public class MyLinkedList<E> extends AbstractSequentialList<E> {
private int size; // 维护此列表的元素个数
private Node<E> first; // 此列表的第一个节点
private Node<E> last; // 此列表的最后一个节点
public MyLinkedList() {
}
@Override
public int size() {
return size;
}
@Override
public ListIterator<E> listIterator(int index) {
return new ListItr(index);
}
// 私有静态的节点类。
private static class Node<E> {
private E item; // 此节点持有的数据
private Node<E> prev;// 上一个节点
private Node<E> next;// 下一个节点
Node(Node<E> prev, E item, Node<E> next) {
this.prev = prev;
this.item = item;
this.next = next;
}
}
// 获取第index个节点。
private Node<E> node(int index) {
if (index < size >> 1) {
Node<E> x = first;
for (int i = 0; i < index; i++) {
x = x.next;
}
return x;
} else {
Node<E> x = last;
for (int i = size - 1; i > index; i--) {
x = x.prev;
}
return x;
}
}
// 迭代器的实现。
private class ListItr implements ListIterator<E> {
private Node<E> lastRet; // 上一次返回的节点
private Node<E> next; // 下一个节点
private int nextIndex; // 下一节点的索引
ListItr(int index) {
// 指定迭代器的开始节点
next = (index >= size) ? null : node(index);
nextIndex = index;
}
@Override
public boolean hasNext() {
return nextIndex < size;
}
@Override
public E next() {
lastRet = next;
next = next.next;
nextIndex++;
return lastRet.item;
}
@Override
public boolean hasPrevious() {
return nextIndex > 0;
}
@Override
public E previous() {
lastRet = next = next.prev;
nextIndex--;
return lastRet.item;
}
@Override
public int nextIndex() {
return nextIndex;
}
@Override
public int previousIndex() {
return nextIndex - 1;
}
@Override
public void remove() {
if (lastRet == null) {
throw new IllegalStateException();
}
unlink(lastRet);
final Node<E> lastNext = lastRet.next;
// ensure the next not be the removed one
if (next == lastRet) {
next = lastNext;
} else {
nextIndex--;
}
lastRet = null;
}
@Override
public void set(E e) {
if (lastRet == null) {
throw new IllegalStateException();
}
lastRet.item = e;
}
@Override
public void add(E e) {
lastRet = null;
if (next == null) {
linkLast(e);
} else {
linkBefore(e, next);
}
nextIndex++;
}
}
/**
* Links e as last element.
*/
private void linkLast(E e) {
final Node<E> l = last;
final Node<E> newNode = new Node<>(l, e, null);
last = newNode;
if (l == null) {
first = newNode;
} else {
l.next = newNode;
}
size++;
}
/**
* Inserts element e before non-null Node next.
*/
private void linkBefore(E e, Node<E> next) {
final Node<E> prev = next.prev;
final Node<E> newNode = new Node<>(prev, e, next);
next.prev = newNode;
if (prev == null) {
first = newNode;
} else {
prev.next = newNode;
}
size++;
}
/**
* unlink node e.
*/
private E unlink(Node<E> x) {
final E val = x.item;
final Node<E> prev = x.prev;
final Node<E> next = x.next;
if (next == null) {
last = prev;
} else {
next.prev = prev;
x.next = null;
}
if (prev == null) {
first = next;
} else {
prev.next = next;
x.prev = null;
}
x.item = null;
size--;
return val;
}
}