Exemplo n.º 1
0
 /** Constructor */
 public DoubleList() {
   head = new DoubleNode<>(null);
   tail = new DoubleNode<>(null);
   head.setNext(tail);
   tail.setPrev(head);
   size = 0;
 }
Exemplo n.º 2
0
 /**
  * Remove the supplied node
  *
  * @param n The node to be removed
  * @return Whether node was removed (If it returns false, the node does not exist in the list)
  */
 private boolean removeNode(DoubleNode<E> n) {
   if (contains(n)) {
     n.getPrev().setNext(n.getNext());
     n.getNext().setPrev(n.getPrev());
     n = null;
     size--;
     return true;
   }
   return false;
 }
Exemplo n.º 3
0
 /**
  * Get the node with the highest value in the list
  *
  * @return The node with the highest item value
  */
 private DoubleNode<E> highest() {
   DoubleNode<E> highest = head.getNext();
   Iterator<DoubleNode<E>> i = nodeIterator();
   while (i.hasNext()) {
     DoubleNode<E> current = i.next();
     if (current.getValue().compareTo(highest.getValue()) > 0) {
       highest = current;
     }
   }
   return highest;
 }
Exemplo n.º 4
0
 /**
  * Remove all items with matching value
  *
  * @param value The value to be removed
  * @return Whether one or more items were removed
  */
 public boolean remove(E value) {
   boolean removed = false;
   Iterator<DoubleNode<E>> i = nodeIterator();
   while (i.hasNext()) {
     DoubleNode<E> current = i.next();
     if (value.equals(current.getValue())) {
       removeNode(current);
       removed = true;
     }
   }
   return removed;
 }
Exemplo n.º 5
0
 /**
  * Add an item before the supplied node
  *
  * @param node The node to add before
  * @param value The value to add
  */
 private void addBefore(DoubleNode<E> node, E value) {
   if (node == head) {
     throw new IllegalArgumentException("Cannot add node before head");
   }
   DoubleNode<E> n = new DoubleNode<>(value);
   node.getPrev().setNext(n);
   n.setPrev(node.getPrev());
   node.setPrev(n);
   n.setNext(node);
   size++;
 }
Exemplo n.º 6
0
 /**
  * Add an item after the supplied node
  *
  * @param node The node to add after
  * @param value The value to add
  */
 private void addAfter(DoubleNode<E> node, E value) {
   if (node == tail) {
     throw new IllegalArgumentException("Cannot add node after tail");
   }
   DoubleNode<E> n = new DoubleNode<>(value);
   node.getNext().setPrev(n);
   n.setNext(node.getNext());
   node.setNext(n);
   n.setPrev(node);
   size++;
 }
Exemplo n.º 7
0
 /**
  * Add an item in rising order
  *
  * @param value The value to be added
  */
 public void add(E value) {
   DoubleNode<E> closest = lowest();
   if (size == 0 || closest.getValue().compareTo(value) > 0) {
     addFirst(value);
     return;
   }
   Iterator<DoubleNode<E>> i = nodeIterator();
   while (i.hasNext()) {
     DoubleNode<E> current = i.next();
     if (current.getValue().compareTo(value) < 0
         && current.getValue().compareTo(closest.getValue()) > 0) {
       closest = current;
     }
   }
   addAfter(closest, value);
 }
Exemplo n.º 8
0
 /**
  * Get the last item in the list
  *
  * @return The last item in the list
  */
 public E getLast() {
   return tail.getPrev().getValue();
 }
Exemplo n.º 9
0
 /**
  * Get the first item in the list
  *
  * @return The first item in the list
  */
 public E getFirst() {
   return head.getNext().getValue();
 }