Java的List ADT的第二种实现方式是LinkedList。该类提供了List ADT的双链表实现。使用LinkedList的优点在于,插入新项与删除现有的项开销均很小,但是get与set开销较大。
在考虑设计方面需要三个类:
1、MyLinkedList类本身,需要包含到两端的链、表的大小及一些方法。
2、Node类。
3、LinkedListIterator实现Iterator接口。
关键代码如下
import java.util.Iterator;
import java.util.NoSuchElementException;
/**
*
* @author sdu20
*
*/
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 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 index,AnyType x){
addBefore(getNode(index,0,size()),x);
}
public AnyType get(int index){
return getNode(index).data;
}
public AnyType set(int index,AnyType newVal){
Node<AnyType> p = getNode(index);
AnyType oldVal = p.data;
p.data = newVal;
return oldVal;
}
public AnyType remove(int index){
return remove(getNode(index));
}
private void addBefore(Node<AnyType> p,AnyType x){
Node<AnyType> newNode = new Node<>(x,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 index){
return getNode(index,0,size()-1);
}
private Node<AnyType> getNode(int index,int lower,int upper){
Node<AnyType> p;
if(index<lower || index>upper)
throw new IndexOutOfBoundsException();
if(index < size()/2){
p = beginMarker.next;
for(int i = 0;i<index;i++)
p = p.next;
}else{
p = endMarker;
for(int i = size(); i>index;i--)
p = p.prev;
}
return p;
}
@Override
public Iterator<AnyType> iterator() {
// TODO Auto-generated method stub
return new LinkedListIterator();
}
private class LinkedListIterator implements Iterator<AnyType>{
private Node<AnyType> current = beginMarker.next;
private int expectedModCount = modCount;
private boolean okToRemove = false;
@Override
public boolean hasNext() {
// TODO Auto-generated method stub
return current != endMarker;
}
@Override
public AnyType next() {
// TODO Auto-generated method stub
if(modCount != expectedModCount)
throw new java.util.ConcurrentModificationException();
if(!hasNext())
throw new NoSuchElementException();
AnyType nextItem = current.data;
current = current.next;
okToRemove = true;
return nextItem;
}
public void remove(){
if(modCount != expectedModCount)
throw new java.util.ConcurrentModificationException();
if(!okToRemove)
throw new IllegalStateException();
MyLinkedList.this.remove(current.prev);
expectedModCount++;
okToRemove = false;
}
}
}