My only problem is that once I write all the methods for the class from the deque class I get an EmptyListException Error which I created. But I don't know why it is being thrown?
Any one with any ideas as to why I would be getting this error?
I've included the NodePositionList Class after the DequeAdapter Class.
here is the DequeAdapter Class
public class DequeAdapter<E> implements Deque<E> {
private int size;
protected NodePositionList<E> E;
public DequeAdapter() {
E = new NodePositionList<E>();
size = 0;
}
/*Returns the number of elements in the dequeAdapter.
**/
public int size()throws EmptyListException{
if(isEmpty())throw new EmptyListException("The list is empty");
return size;
}
/*Returns whether the deque is empty.*/
public boolean isEmpty(){
return (size == 0);
}
/*Inserts a Element to be the first in the deque.*/
public void addFirst(E element){
E.addFirst(element);
}
/*Inserts a Element to be the last in the deque.*/
public void addLast(E element){
E.addLast(element);
}
/*Returns the first element, an exception is thrown if the deque is empty .*/
public E getFirst() throws EmptyDequeException{
if(isEmpty())throw new EmptyListException("The list is empty");
E temp = E.first().element();
return temp;
}
/*Returns the last element, an exception is thrown if the deque is empty .*/
public E getLast() throws EmptyDequeException{
if(isEmpty())throw new EmptyListException("The list is empty");
return E.last().element();
}
/*Removes the first element, an exception is thrown if the deque is empty .*/
public E removeFirst() throws EmptyDequeException{
return E.remove(E.first());
}
/*Removes the last element, an exception is thrown if the deque is empty .*/
public E removeLast() throws EmptyDequeException, InvalidPositionException{
return E.remove(E.last());
}
}
here is NodePositionList Class
public class NodePositionList<E> implements PositionList<E> {
protected int numElts; // Number of elements in the list
protected DNode<E> header, trailer; // Special sentinels
/** Constructor that creates an empty list; O(1) time */
public NodePositionList() {
numElts = 0;
header = new DNode<E>(null, null, null); // create header
trailer = new DNode<E>(header, null, null); // create trailer
header.setNext(trailer); // make header and trailer point to each other
}
/** Checks if position is valid for this list and converts it to
* DNode if it is valid; O(1) time */
protected DNode<E> checkPosition(Position<E> 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<E> temp = (DNode<E>) 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 list");
}
}
/** Returns the number of elements in the list; O(1) time */
public int size() { return numElts; }
/** Returns whether the list is empty; O(1) time */
public boolean isEmpty() { return (numElts == 0); }
/** Returns the first position in the list; O(1) time */
public Position<E> first()throws EmptyListException {
if (isEmpty())throw new EmptyListException("List is empty");
return header.getNext();
}
/* Returns the last position in the list; O(1) time*/
public Position<E> last()throws EmptyListException{
if(isEmpty())throw new EmptyListException("List is empty");
return trailer.getPrev();
}
/** Returns the position before the given one; O(1) time */
public Position<E> prev(Position<E> p)throws InvalidPositionException, BoundaryViolationException {
DNode<E> v = checkPosition(p);
DNode<E> prev = v.getPrev();
if (prev == header)throw new BoundaryViolationException
("Cannot advance past the beginning of the list");
return prev;
}
/*Returns the position after the given one; O(1) time*/
public Position<E> next(Position<E> p)throws InvalidPositionException, BoundaryViolationException{
DNode<E> t = checkPosition(p);
DNode<E> next = t.getNext();
if(next == trailer)throw new BoundaryViolationException
("Cannot advance past the end of the list.");
return next;
}
/** Insert the given element before the given position, returning
* the new position; O(1) time */
public void addBefore(Position<E> p, E element)throws InvalidPositionException {
DNode<E> v = checkPosition(p);
numElts++;
DNode<E> newNode = new DNode<E>(v.getPrev(), v, element);
v.getPrev().setNext(newNode);
v.setPrev(newNode);
}
/** Insert the given element at the beginning of the list, returning
* the new position; O(1) time */
public void addFirst(E element) {
numElts++;
DNode<E> newNode = new DNode<E>(header, header.getNext(), element);
header.getNext().setPrev(newNode);
header.setNext(newNode);
}
/*Inserts the given element at the end of the list, returning
*the new position; O(1)time*/
public void addLast(E element){
numElts++;
DNode<E> newNode = new DNode<E>(trailer.getPrev(), trailer, element);
trailer.getPrev().setNext(newNode);
trailer.setPrev(newNode);
}
/*Inserts the given element after the given position*/
public void addAfter(Position<E> p, E element)throws InvalidPositionException{
DNode<E> t = checkPosition(p);
numElts++;
DNode<E> newNode = new DNode<E>(t, t.getNext(), element);
t.getNext().setPrev(newNode);
t.setNext(newNode);
}
/**Remove the given position from the list; O(1) time */
public E remove(Position<E> p)throws InvalidPositionException {
DNode<E> v = checkPosition(p);
numElts--;
DNode<E> vPrev = v.getPrev();
DNode<E> vNext = v.getNext();
vPrev.setNext(vNext);
vNext.setPrev(vPrev);
E vElem = v.element();
// unlink the position from the list and make it invalid
v.setNext(null);
v.setPrev(null);
return vElem;
}
/** Replace the element at the given position with the new element
* and return the old element; O(1) time */
public E set(Position<E> p, E element)throws InvalidPositionException {
DNode<E> v = checkPosition(p);
E oldElt = v.element();
v.setElement(element);
return oldElt;
}
}
Edited by macosxnerd101: Please,

New Topic/Question
Reply




MultiQuote






|