本来是要上传文件的,不过没成功,现在改为贴代码了,这个class 已经写得很经典了,你可以直接拿来用
代码如下:
import java.util.Enumeration;
class MyAccessEmptyContainerException extends Exception
{
public MyAccessEmptyContainerException(){}
public MyAccessEmptyContainerException(String errorMsg)
{
System.out.println(errorMsg);
}
}
public class DLList
{
class DLNode
{
private Object element;
private DLNode prev, next;
public DLNode(Object o)
{
element = o;
prev = next = null;
}
public DLNode(Object o, DLNode n, DLNode p)
{
element = o;
next = n;
prev = p;
}
public Object getElement(){return element;}
public DLNode getPrev(){ return prev;}
public DLNode getNext(){ return next;}
public void setElement(Object o){element = o;}
public void setPrev(DLNode p){ prev = p;}
public void setNext(DLNode n){ next = n;}
public final synchronized void remove()
{
if(this != endNode)
{
next.setPrev(prev);
prev.setNext(next);
next = null;
prev = null;
elementCount--;
}
}
}
public final int size(){return elementCount;}
public final synchronized void makeEmpty()
{
endNode.setNext(endNode);
endNode.setPrev(endNode);
elementCount = 0;
}
public final boolean isEmpty(){ return size()<= 0;}
public synchronized Object clone()
{
DLList d = new DLList();
for(Enumeration e = elements(); e.hasMoreElements(); )
d.addElement(e.nextElement());
return d;
}
public final synchronized Enumeration elements()
{
return new Enumeration()
{
public boolean hasMoreElements()
{
return current != endNode;
}
public synchronized Object nextElement()
{
Object returnValue = current.getElement();
current = current.getNext();
return returnValue;
}
protected DLNode current = endNode.getNext();
};
}
public final synchronized boolean contains(final Object o)
{
return indexOf(o) >= 0;
}
public final synchronized int indexOf(final Object o)
{
if(!isEmpty())
{
DLNode n = endNode.getNext();
for(int i = 0; (n!=endNode); i++, n = n.getNext())
{
if(n.getElement().equals(o))
return i;
}
}
return -1;
}
public final synchronized Object elementAt(final int index)
{
return nodeAt(index).getElement();
}
public final synchronized Object firstElement() throws MyAccessEmptyContainerException
{
if(isEmpty())
throw new MyAccessEmptyContainerException();
return endNode.getNext().getElement();
}
public final synchronized Object lastElement() throws MyAccessEmptyContainerException
{
if(isEmpty())
throw new MyAccessEmptyContainerException();
return endNode.getPrev().getElement();
}
public synchronized void setElementAt(final Object o, final int index)
{
nodeAt(index).setElement(o);
}
public final synchronized void removeElementAt(final int index)
{
nodeAt(index).remove();
}
public synchronized void insertElementAt(final Object o, final int index)
{
checkIndex(index);
if(isEmpty() && (index == 0))
addElement(o);
else
{
DLNode current = nodeAt(index);
DLNode n = new DLNode(o, current, current.getPrev());
current.getPrev().setNext(n);
current.setPrev(n);
elementCount++;
}
}
public synchronized void addElement(final Object o)
{
DLNode n = new DLNode(o);
DLNode prev = endNode.getPrev();
if(size() == 0)
{
n.setNext(endNode);
n.setPrev(endNode);
endNode.setPrev(n);
endNode.setNext(n);
}
else
{
prev.setNext(n);
n.setPrev(prev);
n.setNext(endNode);
endNode.setPrev(n);
}
elementCount++;
}
public final synchronized boolean removeElement(final Object o)
{
int index = indexOf(o);
if(index < 0)
return false;
nodeAt(index).remove();
return true;
}
public final synchronized void removeAllElements()
{
if(!isEmpty())
makeEmpty();
}
public final Object head() throws MyAccessEmptyContainerException
{
if(isEmpty())
throw new MyAccessEmptyContainerException();
return endNode.getNext().getElement();
}
public final synchronized DLList tail() throws MyAccessEmptyContainerException
{
if(isEmpty())
throw new MyAccessEmptyContainerException();
DLList d = (DLList)clone();
d.removeElementAt(0);
return d;
}
public final synchronized String toString()
{
StringBuffer sb = new StringBuffer();
Enumeration e = elements();
sb.append("[");
while(e.hasMoreElements())
{
sb.append(e.nextElement().toString());
if(e.hasMoreElements())
sb.append(",");
}
sb.append("]");
return sb.toString();
}
private final synchronized void checkIndex(final int index)
{
if(index >= size())
throw new IndexOutOfBoundsException
("DLList " + index + " >= " + size());
if(index < 0 )
throw new IndexOutOfBoundsException
("DLList " + index + " < 0");
}
private final synchronized DLNode nodeAt(final int index)
{
checkIndex(index);
DLNode current = endNode.getNext();
if(index >= size()/2)
{
current = endNode.getPrev();
for(int i = size() - 1; i>index; i--)
current = current.getPrev();
}
else
{
for(int i = 0; i<index; i++)
current = current.getNext();
}
return current;
}
public DLNode dlnodeAtPosition(int p)
{
return nodeAt(p);
}
private final DLNode endNode = new DLNode(null);
{
makeEmpty();
}
private DLNode node;
private int elementCount = 0;
public DLList() {
}
}