Пример #1
0
  public void insertAfter(final LinkedListNode existingNode, final LinkedListNode newNode) {
    if (newNode.getPrevious() != null || newNode.getNext() != null) {
      // do nothing if this node is already inserted somewhere
      return;
    }

    if (existingNode == null) {
      if (this.isEmpty()) {
        this.firstNode = newNode;
        this.lastNode = newNode;
      } else {
        // if existing node is null, then insert it as a first node
        final LinkedListNode node = this.firstNode;
        node.setPrevious(newNode);
        newNode.setNext(node);
        this.firstNode = newNode;
      }
    } else if (existingNode == this.lastNode) {
      existingNode.setNext(newNode);
      newNode.setPrevious(existingNode);
      this.lastNode = newNode;
    } else {
      (existingNode.getNext()).setPrevious(newNode);
      newNode.setNext(existingNode.getNext());
      existingNode.setNext(newNode);
      newNode.setPrevious(existingNode);
    }
    this.size++;
  }
Пример #2
0
 /**
  * Add a <code>LinkedListNode</code> to the list. If the <code>LinkedList</code> is empty then the
  * first and last nodes are set to the added node.
  *
  * @param node The <code>LinkedListNode</code> to be added
  */
 public void add(final LinkedListNode node) {
   if (this.firstNode == null) {
     this.firstNode = node;
     this.lastNode = node;
     ;
   } else {
     this.lastNode.setNext(node);
     node.setPrevious(this.lastNode);
     this.lastNode = node;
   }
   this.size++;
 }
Пример #3
0
 /**
  * Removes a <code>LinkedListNode</code> from the list. This works by attach the previous
  * reference to the child reference. When the node to be removed is the first node it calls <code>
  * removeFirst()</code>. When the node to be removed is the last node it calls <code>removeLast()
  * </code>.
  *
  * @param node The <code>LinkedListNode</code> to be removed.
  */
 public void remove(final LinkedListNode node) {
   if (this.firstNode == node) {
     removeFirst();
   } else if (this.lastNode == node) {
     removeLast();
   } else {
     node.getPrevious().setNext(node.getNext());
     (node.getNext()).setPrevious(node.getPrevious());
     this.size--;
     node.setPrevious(null);
     node.setNext(null);
   }
 }
Пример #4
0
 /**
  * Remove the last node from the list. The previous node then becomes the last node. If this is
  * the last node then both first and last node references are set to null.
  *
  * @return The first <code>LinkedListNode</code>.
  */
 public LinkedListNode removeLast() {
   if (this.lastNode == null) {
     return null;
   }
   final LinkedListNode node = this.lastNode;
   this.lastNode = node.getPrevious();
   node.setPrevious(null);
   if (this.lastNode != null) {
     this.lastNode.setNext(null);
   } else {
     this.firstNode = this.lastNode;
   }
   this.size--;
   return node;
 }