this is my version of linked list don't laugh now
<code>
import java.util.NoSuchElementException;
public class ListTest {
private Node start,end,data,cursor;
private Object next,previouse;
private static int size;
protected final Object START = start;
protected final Object END = end;
private class Node {
private Node next;
private Node previouse;
private Object data;
private void setNext( Object o ) {
next = ( Node )o;
}
private void setPreviouse( Object o ) {
previouse = ( Node )o;
}
private void setData( Object o ) {
data = o;
}
private Node getNext() { return next;}
private Node getPreviouse() { return previouse;}
private Object getData() { return data;}
}
public void addElement( Object o ) {
if ( isEmpty() ) {
start = end = new Node();
start.setData( o );
}
else {
Node temp = end;
end = new Node();
setCursor( temp );
getCursor().setNext( end );
end.setPreviouse( temp );
}
end.setData( o );
setCursor( end );
size++;
}
private void setCursor( Node current ) { cursor = current; }
private Node getCursor() { return cursor; }
public boolean isEmpty() { return getSize() == 0; }
public Object getLast() { return end.getData(); }
public Object getFirst() { return start.getData(); }
public Object previouseElement() throws NoSuchElementException {
if ( getSize() == 0 ) throw new NoSuchElementException(
"No Previouse Record");
if ( getCursor().getPreviouse() == null )
setCursor( end );
else
setCursor( getCursor().getPreviouse() );
return getCursor().getData();
}
public Object nextElement() throws NoSuchElementException {
if ( getSize() == 0 ) throw new NoSuchElementException(
"No Next Record");
if ( getCursor().getNext() == null )
setCursor( start );
else
setCursor( getCursor().getNext() );
return getCursor().getData();
}
public void reset( Object pos ) throws NoSuchElementException {
if ( getCursor() == null ) throw new NoSuchElementException(
"No Record");
setCursor( ( Node )pos );
}
public boolean hasMoreElements() {
return getCursor().getNext() != null;
}
public void remove() throws NoSuchElementException {
if ( getCursor() == null ) throw new NoSuchElementException(
"No Records");
if ( ( getCursor().getPreviouse() != null ) && ( getCursor().getNext() != null ) ) {
Node prev = getCursor().getPreviouse();
Node next = getCursor().getNext();
next.setPreviouse( prev );
prev.setNext( next );
setCursor( next );
}
else {
if ( getCursor().getPreviouse() == null && getSize() > 1) {
start = getCursor().getNext();
start.setPreviouse( null );
setCursor( start );
}
else if ( getCursor().getNext() == null && getSize() > 1) {
end = getCursor().getPreviouse();
end.setNext( null );
setCursor( end );
}
else
start = end = cursor = null;
}
size--;
}
public Object currentElement() throws NoSuchElementException {
if ( getCursor() == null ) throw new NoSuchElementException(
"No Corrent Element");
return getCursor().getData();
}
public int getSize() {
return size;
}
}
</code>