// Constructor; O(1) time
public NodeList() {
numElts = 0;
header = new DNode(null, null, null); // create header
trailer = new DNode(header, null, null); // create trailer
header.setNext(trailer); // make header and trailer point to each other
}
// Convenience function; O(1) time
protected DNode checkPosition(Position p)
throws InvalidPositionException {
if (p == null)
throw new InvalidPositionException
("Null Position passed to NodeList.");
if (p == header)
throw new InvalidPositionException
("The header node is not a valid position");
if (p == trailer)
throw new InvalidPositionException
("The trailer node is not a valid position");
try {
DNode temp = (DNode)p;
if ((temp.getPrev() == null) || (temp.getNext() == null))
throw new InvalidPositionException
("Position does not belong to a valid NodeList");
return temp;
} catch (ClassCastException e) {
throw new InvalidPositionException
("Position is of wrong type for this container.");
}
}
// Simple accessor methods:
public int size() { return numElts; } // O(1) time
public boolean isEmpty() { return (numElts < 1); } // O(1) time
public boolean isFirst(Position p) // O(1) time
throws InvalidPositionException {
DNode v = checkPosition(p);
return v.getPrev() == header;
}