Add After
by Isai Damier

/***************************************************************************
 * Author: Isai Damier
 * Title: Singly Linked List
 * Project: geekviewpoint
 * Package: datastructure
 *
 * Description: A LinkedList is a data structure that allows access
 *   to a collection of data using pointers/references. While an
 *   array can also be defined as above, LinkedLists and arrays differ
 *   in how they are stored in memory and in the operations they
 *   allow. Unlike an array that must be stored in a block of memory,
 *   the nodes of a LinkedList can be stored anywhere because each
 *   node has a reference to the node that succeeds it. Because the
 *   nodes are stored so loosely, inserting nodes into a LinkedList
 *   is easy; whereas in an array, all the succeeding elements must
 *   be shifted. Of course, insertion also means changing the size of
 *   the array, which means creating the entire array anew.
 *
 *   Perhaps the greatest beauty of LinkedList is that it allows
 *   accessing an entire sequence of nodes using only one variable:
 *   a reference to the first node in the sequence.
 *
 *   Countless operations can be performed on LinkedLists. Following
 *   are a few, ranging from the common to the very interesting.
 **************************************************************************/ 
 public class SinglyLinkedList {

  Node head = null;
  Node tail = null;

  /*****************************************************************
   * Statement:
   *   Add element el after node n.
   *
   * Time Complexity of Solution:
   *   Best = O(1); Worst = O(n).
   *
   * Description:
   *
   * Technical Details: We simply need to find node n on the list
   *   and then attach the new node containing el to said node, which
   *   we represent as tmp. If n is not found, then el is not added.
   *   instead of having a traversal algorithm all the time, we treat the
   *   possibility of n being the tail of the list as a special case
   *   where the problem could be solved in O(1)
   *****************************************************************/
  public void addAfter(int el, Node n) {
    if (null == tail || n.data == tail.data) {
      addToTail(el);
    } else {
      Node tmp = head;
      for (; null != tmp && n.data != tmp.data; tmp = tmp.next);
      if (null != tmp) {
        tmp.next = new Node(el, tmp.next);
      }
    }
  }
}
public class SinglyLinkedListTest {

  /**
   * Test of addAfter method, of class SinglyLinkedList.
   */
  @Test
  public void testAddAfter() {
    System.out.println("addAfter");
    int[] input = {9, 4, 5, 2, 1, 12, 6, 7, 4, 8, 3, 0, 16, 19, 11};
    SinglyLinkedList linkedList = new SinglyLinkedList();
    for (int i = 0; i < input.length; i++) {
      linkedList.addToTail(input[i]);
    }
    int value = 51;
    linkedList.addAfter(value, linkedList.find(7));
    int[] expected = {9, 4, 5, 2, 1, 12, 6, 7, 51, 4, 8, 3, 0, 16, 19, 11};
    assertTrue(Arrays.equals(expected, linkedList.toArray()));
  }
}