public void insertBefore(object data, object newData) { if (head == null) { return; } if (head.getData() == data) { addFirst(newData); return; } LinkedListNode prev = null; LinkedListNode curr = head; while ((curr != null) && (curr.getData() != data)) { prev = curr; curr = curr.getNextNode(); } if (curr != null) { prev.setNextNode(new LinkedListNode(newData, curr)); } }
public void remove(object data) { if (head == null) { throw new Exception("List is empty, cannot delete."); } if (head.getData() == data) { head = head.getNextNode(); return; } LinkedListNode prev = null; LinkedListNode curr = head; while ((curr != null) && (curr.getData() != data)) { prev = curr; curr = curr.getNextNode(); } if (curr == null) { throw new Exception("Cannot delete."); } prev.setNextNode(curr.getNextNode()); }
public void insertAfter(object data, object newData) { LinkedListNode temp = head; while ((temp != null) && (temp.getData() != data)) { temp = temp.getNextNode(); } if (temp != null) { temp.setNextNode(new LinkedListNode(newData, temp)); } }