コード例 #1
0
  /*
   * Remove the nth element in the list.  The first element is element 1.
   * Return the removed element to the caller.
   */
  public E remove(int n) {
    LinkEntry<E> le = new LinkEntry<E>();
    le = head;

    if (n == 1) {
      if (size() == 1) {
        head = tail = null;
        return le.element;
      } else {
        head = head.next;
        head.previous = null;
        return le.element;
      }
    } else if (n == size()) {
      le = tail;
      tail = tail.previous;
      tail.next = null;
      return le.element;
    } else {
      for (int i = 1; i < n; i++) le = le.next;
      le.previous.next = le.next;
      le.next.previous = le.previous;
      return le.element;
    }
  }
コード例 #2
0
 /*
  * Add e to the end of the doubly linked list.
  * Returns true - if e was successfully added, false otherwise.
  */
 public boolean add(E e) {
   int length = size();
   LinkEntry<E> le = new LinkEntry<E>();
   le.element = e;
   if (head == null) head = tail = le;
   else {
     tail.next = le;
     le.previous = tail;
     tail = le;
   }
   if (size() - length == 1) return true;
   else return false;
 }