public Comparable remove(int index) {
    checkIndex(index); // �ndice v�lido

    DLNode aux = getNode(index);
    Comparable removedElement = aux.getElement();

    if (index == 0) {
      cabeca = (DLNode) aux.getNext();
      if (cabeca == null) cauda = null;
      else cabeca.setPrev(null);

      aux.setNext(null);
    } else {
      Node prev = aux.getPrev();
      Node prox = aux.getNext();

      prev.setNext(prox);

      if (prox != null) ((DLNode) prox).setPrev(prev);
      else cauda = (DLNode) prev;

      aux.setNext(null);
      aux.setPrev(null);
    }
    size--;
    return removedElement;
  }
Esempio n. 2
0
 /**
  * Returns a string representation of the contents of the linked list, from the front to the back.
  *
  * @return a string representation of the linked list, from front to back
  */
 public String toString() {
   if (isEmpty()) {
     return "";
   } else if (getHead() == getTail()) {
     // one element
     return getHead().getElement().toString();
   } else {
     StringBuilder newString = new StringBuilder();
     DLNode<T> placeHolder = getHead();
     // loop through and build string node by node
     while (placeHolder.getNext() != null) {
       newString.append(placeHolder.getElement().toString());
       placeHolder = placeHolder.getNext();
     }
     newString.append(placeHolder.getElement().toString());
     return newString.toString();
   }
 }
Esempio n. 3
0
 /**
  * Remove and return the element at the back of the linked list.
  *
  * @return the element that was at the back of the linked list
  * @throws NoSuchElementException if attempting to remove from an empty list
  */
 public T removeFromBack() {
   if (isEmpty()) {
     throw new NoSuchElementException();
   }
   DLNode<T> oldTail = getTail();
   if (getTail().getPrevious() != null) {
     // fix next for new tail and set new tail
     getTail().getPrevious().setNext(null);
     setTail(getTail().getPrevious());
   } else {
     // removed last node
     setTail(null);
     setHead(null);
   }
   return oldTail.getElement();
 }
Esempio n. 4
0
 /**
  * Remove and return the element at the front of the linked list.
  *
  * @return the element that was at the front of the linked list
  * @throws NoSuchElementException if attempting to remove from an empty list
  */
 public T removeFromFront() {
   if (isEmpty()) {
     throw new NoSuchElementException();
   }
   DLNode<T> oldHead = getHead();
   if (getHead().getNext() != null) {
     // fix previous for new head and set new head
     getHead().getNext().setPrevious(null);
     setHead(getHead().getNext());
   } else {
     // removed last node
     setHead(null);
     setTail(null);
   }
   return oldHead.getElement();
 }