LinkedList类的底层实现-Java
package MyLinkedList;
import javax.script.AbstractScriptEngine;
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>{
private AnyType data;
private Node<AnyType> prev;
private Node<AnyType> next;
public Node(AnyType x, Node<AnyType> p, Node<AnyType> n) {
data = x;
prev = p;
next = n;
}
}
public MyLinkedList() {
doClear();
}
public void clear() {
doClear();
}
private void doClear() {
beginMarker = new Node<AnyType>(null ,null, null);
endMarker = new Node<AnyType>(null, beginMarker, null);
beginMarker.next = endMarker;
theSize = 0;
modCount++;
}
public int size() {
return theSize;
}
public boolean isEmpty() {
return size()==0;
}
public boolean add(AnyType x) {
add(size(), x);
return true;
}
public void add(int idx, AnyType x) {
addBefore(getNode(idx, 0, size()), x);
}
public AnyType get(int idx) {
return getNode(idx).data;
}
public AnyType set(int idx, AnyType x) {
Node<AnyType> p = getNode(idx);
AnyType oldVal = p.data;
p.data = x;
return oldVal;
}
public AnyType remove(int idx) {
return remove(getNode(idx));
}
private AnyType remove(Node<AnyType> p) {
p.next.prev = p.prev;
p.prev.next = p.next;
theSize--;
modCount++;
return p.data;
}
private void addBefore(Node<AnyType> p, AnyType x) {
Node<AnyType> newNode = new Node<AnyType>(x, p.prev, p);
newNode.prev.next = newNode;
p.prev = newNode;
theSize++;
modCount++;
}
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;
for (int i = size(); i > idx; i--) {
p = p.prev;
}
}
return p;
}
public java.util.Iterator<AnyType> iterator() {
return new LinkedListIterator();
}
private class LinkedListIterator implements java.util.Iterator<AnyType>{
private Node<AnyType> current = beginMarker.next;
private int exceptedModCount = modCount;
private boolean okToRmove = false;
public boolean hasNext() {
return current!=endMarker;
}
public AnyType next() {
if (modCount != exceptedModCount) {
throw new java.util.ConcurrentModificationException();
}
if (!hasNext()) {
throw new java.util.NoSuchElementException();
}
AnyType nextItem = current.data;
current = current.next;
okToRmove = true;
return nextItem;
}
public void remove() {
if (modCount != exceptedModCount) {
throw new java.util.ConcurrentModificationException();
}
if (!hasNext()) {
throw new java.util.NoSuchElementException();
}
MyLinkedList.this.remove(current.prev);
exceptedModCount++;
okToRmove = false;
}
}
}
测试
package MyLinkedList;
public class MyLinkedListTest {
public static void main(String[] args) {
MyLinkedList<String> mll = new MyLinkedList<String>();
mll.add("a");
mll.add("b");
mll.add("c");
for (String s : mll) {
System.out.println(s);
}
mll.set(2, "cc");
String str = mll.get(2);
System.out.println(str);
System.out.println(mll.size());
System.out.println(mll.isEmpty());
mll.remove(1);
for (String s : mll) {
System.out.println(s);
}
System.out.println("clear");
mll.clear();
for (String s : mll) {
System.out.println(s);
}
}
}