Exemple #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++;
  }
 /** Insert a new node with data after currentNode */
 public void insertAfter(LinkedListNode<T> currentNode, T data) {
   // create a new linkedListNode
   LinkedListNode<T> newNode = new LinkedListNode<T>();
   // assign that node some data
   newNode.setData(data);
   // point new node where current pointer is
   newNode.setNext(currentNode.getNext());
   // set head pointer to this new node
   currentNode.setNext(newNode);
 }
 /** Remove tail node */
 public void deleteLast() {
   // set a LinkedListNode to get the current Node
   LinkedListNode<T> currentNode = head;
   // when the next to next node is null
   while (currentNode.getNext().getNext() != null) {
     currentNode = currentNode.getNext();
   }
   // set the current node to be null
   currentNode.setNext(null);
 }
Exemple #4
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);
   }
 }
 public static LinkedListNode NumbersAddition(LinkedListNode n1, LinkedListNode n2) {
   // number 1 and number 2 may not have the same number of digits.
   if (n1 == null || n2 == null) return null;
   LinkedListNode sumHead = null;
   LinkedListNode sumTail = null;
   int advance = 0;
   while (n1 != null || n2 != null) {
     int digitSum = 0;
     if (n1 != null && n2 != null) digitSum = n1.getData() + n2.getData() + advance;
     else if (n1 == null) digitSum = n2.getData() + advance;
     else digitSum = n1.getData() + advance;
     if (digitSum >= 10) {
       advance = 1;
       digitSum = digitSum % 10;
     } else {
       advance = 0;
     }
     LinkedListNode dSum = new LinkedListNode();
     dSum.setData(digitSum);
     if (sumHead == null) sumHead = dSum;
     if (sumTail == null) {
       sumTail = dSum;
     } else {
       sumTail.setNext(dSum);
       sumTail = dSum;
     }
     // move n1 and n2 under certain conditions!
     if (n1 != null) n1 = n1.getNext();
     if (n2 != null) n2 = n2.getNext();
   }
   if (advance == 1) {
     LinkedListNode last = new LinkedListNode();
     last.setData(advance);
     sumTail.setNext(last);
     sumTail = last;
   }
   return sumHead;
 }
Exemple #6
0
 /**
  * Remove the first node from the list. The next node then becomes the first 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 removeFirst() {
   if (this.firstNode == null) {
     return null;
   }
   final LinkedListNode node = this.firstNode;
   this.firstNode = node.getNext();
   node.setNext(null);
   if (this.firstNode != null) {
     this.firstNode.setPrevious(null);
   } else {
     this.lastNode = null;
   }
   this.size--;
   return node;
 }
  public void join(LinkedListNode node1, LinkedListNode node2) {

    LinkedListNode lastNode = getLastNode(node1);
    lastNode.setNext(node2);
  }
 /** Insert the node at the head of the list */
 public void insertFirstNode(LinkedListNode<T> node) {
   // have the new node point to the old head
   node.setNext(head);
   // Set the new node as the head
   head = node;
 }
 /**
  * Remove node following currentNode If no node exists (i.e., currentNode is the tail), do nothing
  */
 public void deleteNext(LinkedListNode<T> currentNode) {
   // update pointer of the current node to the next next
   currentNode.setNext(currentNode.getNext().getNext());
 }