| /** A simple linked list which use an iterator instead of revealing of the Linkable's */
public class LinkedList {
private Linkable firstLinkable;
private int length;
private class Linkable {
private Object data;
private Linkable next;
/** Return a Linkable object with null references in both data and next */
Linkable(){
data = null;
next = null;
}
/** Return a Linkable object with null references in next, and the parameter as data */
Linkable(Object data){
this.data = data;
this.next = null;
}
/** Return a Linkable object with first parameter as data and second parameter as next */
Linkable(Object data, Linkable next){
this.data = data;
this.next = next;
}
/** Return the data of this Linkable object */
Object data(){
return (data);
}
/** Return the reference to the next Linkable */
Linkable next(){
return (next);
}
/** Set the data field of this Linkable object */
void setData(Object data){
this.data = data;
}
/** Set the next field of this Linkable object */
void setNext(Linkable next){
this.next = next;
}
} // Linkable
public class LinkedListEnumeration implements java.util.Enumeration {
private LinkedList enumeratedList;
private Linkable currentLinkable;
public LinkedListEnumeration() {
enumeratedList = LinkedList.this;
currentLinkable = firstLinkable;
}
public boolean hasMoreElements(){
return(currentLinkable != null);
}
public Object nextElement(){
Object result = currentLinkable.data();
currentLinkable = currentLinkable.next();
return (result);
}
} // LinkedListEnumeration
public LinkedList(){
firstLinkable = null;
length = 0;
}
public void insertFirst(Object element){
Linkable newFirst = new Linkable(element, firstLinkable);
firstLinkable = newFirst;
length = length + 1;
}
public void deleteFirst(){
if (length > 0){
firstLinkable = firstLinkable.next();
length = length - 1;
}
}
public java.util.Enumeration elements(){
return new LinkedListEnumeration();
}
public int length(){
return length;
}
} |