/** * Inserts the given node z before the given node v. And error occurs if v is the header * * @param v * @param z * @throws IllegalArgumentException */ public void addBefore(DLNode<E> v, DLNode<E> z) throws IllegalArgumentException { DLNode<E> u = getPrev(v); // may throw an IllegalArgumentException z.setPrev(u); z.setNext(v); v.setPrev(z); u.setNext(z); self.size++; }
/** * Inserts the given node z after the given node v. And error occurs if v is the trailer * * @param v * @param z * @throws IllegalArgumentException */ public void addAfter(DLNode<E> v, DLNode<E> z) throws IllegalArgumentException { DLNode<E> w = getNext(v); // may throw an IllegalArgumentException z.setPrev(v); z.setNext(w); w.setPrev(z); v.setNext(z); self.size++; }
/** * Returns a string representation of the list * * @return s string representation of the list */ @Override public String toString() { String s = "[ "; DLNode<E> v = self.header.getNext(); while (v != self.trailer) { s += v.getElement(); v = v.getNext(); if (v != self.trailer) { s += ", "; } } s += " ]"; return s; }
/** * Removes the given node v from the list. An error occurs if v is the header or tailer * * @param v */ public void remove(DLNode<E> v) { DLNode<E> u = self.getPrev(v); // may throw an IllegalArgumentException DLNode<E> w = self.getNext(v); // may throw an IllegalArgumentException // unlink the node from the list w.setPrev(u); u.setNext(w); v.setPrev(null); v.setNext(null); self.size--; }
/** * Returns the node after the given node v. An error occurs if v is the trailer * * @param v the current node we are looking to get the next node of */ public DLNode<E> getNext(DLNode<E> v) throws IllegalArgumentException { if (v == self.trailer) { throw new IllegalArgumentException("Cannot move forward past the trailer of the list"); } return v.getNext(); }
/** * Returns the node before the given node v. An error occurs if v is the header * * @param v the current node we are looking to get the previous node of */ public DLNode<E> getPrev(DLNode<E> v) throws IllegalArgumentException { if (v == self.header) { throw new IllegalArgumentException("Cannot move back past the header of the list"); } return v.getPrev(); }